sharp.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2013 Lovell Fuller and others.
  2. // SPDX-License-Identifier: Apache-2.0
  3. 'use strict';
  4. const platformAndArch = require('./platform')();
  5. /* istanbul ignore next */
  6. try {
  7. module.exports = require(`../build/Release/sharp-${platformAndArch}.node`);
  8. } catch (err) {
  9. // Bail early if bindings aren't available
  10. const help = ['', 'Something went wrong installing the "sharp" module', '', err.message, '', 'Possible solutions:'];
  11. if (/dylib/.test(err.message) && /Incompatible library version/.test(err.message)) {
  12. help.push('- Update Homebrew: "brew update && brew upgrade vips"');
  13. } else {
  14. const [platform, arch] = platformAndArch.split('-');
  15. if (platform === 'linux' && /Module did not self-register/.test(err.message)) {
  16. help.push('- Using worker threads? See https://sharp.pixelplumbing.com/install#worker-threads');
  17. }
  18. help.push(
  19. '- Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"',
  20. `- Install for the current ${platformAndArch} runtime: "npm install --platform=${platform} --arch=${arch} sharp"`
  21. );
  22. }
  23. help.push(
  24. '- Consult the installation documentation: https://sharp.pixelplumbing.com/install'
  25. );
  26. // Check loaded
  27. if (process.platform === 'win32' || /symbol/.test(err.message)) {
  28. const loadedModule = Object.keys(require.cache).find((i) => /[\\/]build[\\/]Release[\\/]sharp(.*)\.node$/.test(i));
  29. if (loadedModule) {
  30. const [, loadedPackage] = loadedModule.match(/node_modules[\\/]([^\\/]+)[\\/]/);
  31. help.push(`- Ensure the version of sharp aligns with the ${loadedPackage} package: "npm ls sharp"`);
  32. }
  33. }
  34. throw new Error(help.join('\n'));
  35. }