sharp.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*!
  2. Copyright 2013 Lovell Fuller and others.
  3. SPDX-License-Identifier: Apache-2.0
  4. */
  5. // Inspects the runtime environment and exports the relevant sharp.node binary
  6. const { familySync, versionSync } = require('detect-libc');
  7. const { runtimePlatformArch, isUnsupportedNodeRuntime, prebuiltPlatforms, minimumLibvipsVersion } = require('./libvips');
  8. const runtimePlatform = runtimePlatformArch();
  9. const paths = [
  10. `../src/build/Release/sharp-${runtimePlatform}.node`,
  11. '../src/build/Release/sharp-wasm32.node',
  12. `@img/sharp-${runtimePlatform}/sharp.node`,
  13. '@img/sharp-wasm32/sharp.node'
  14. ];
  15. /* node:coverage disable */
  16. let path, sharp;
  17. const errors = [];
  18. for (path of paths) {
  19. try {
  20. sharp = require(path);
  21. break;
  22. } catch (err) {
  23. errors.push(err);
  24. }
  25. }
  26. if (sharp && path.startsWith('@img/sharp-linux-x64') && !sharp._isUsingX64V2()) {
  27. const err = new Error('Prebuilt binaries for linux-x64 require v2 microarchitecture');
  28. err.code = 'Unsupported CPU';
  29. errors.push(err);
  30. sharp = null;
  31. }
  32. if (sharp) {
  33. module.exports = sharp;
  34. } else {
  35. const [isLinux, isMacOs, isWindows] = ['linux', 'darwin', 'win32'].map(os => runtimePlatform.startsWith(os));
  36. const help = [`Could not load the "sharp" module using the ${runtimePlatform} runtime`];
  37. errors.forEach(err => {
  38. if (err.code !== 'MODULE_NOT_FOUND') {
  39. help.push(`${err.code}: ${err.message}`);
  40. }
  41. });
  42. const messages = errors.map(err => err.message).join(' ');
  43. help.push('Possible solutions:');
  44. // Common error messages
  45. if (isUnsupportedNodeRuntime()) {
  46. const { found, expected } = isUnsupportedNodeRuntime();
  47. help.push(
  48. '- Please upgrade Node.js:',
  49. ` Found ${found}`,
  50. ` Requires ${expected}`
  51. );
  52. } else if (prebuiltPlatforms.includes(runtimePlatform)) {
  53. const [os, cpu] = runtimePlatform.split('-');
  54. const libc = os.endsWith('musl') ? ' --libc=musl' : '';
  55. help.push(
  56. '- Ensure optional dependencies can be installed:',
  57. ' npm install --include=optional sharp',
  58. '- Ensure your package manager supports multi-platform installation:',
  59. ' See https://sharp.pixelplumbing.com/install#cross-platform',
  60. '- Add platform-specific dependencies:',
  61. ` npm install --os=${os.replace('musl', '')}${libc} --cpu=${cpu} sharp`
  62. );
  63. } else {
  64. help.push(
  65. `- Manually install libvips >= ${minimumLibvipsVersion}`,
  66. '- Add experimental WebAssembly-based dependencies:',
  67. ' npm install --cpu=wasm32 sharp',
  68. ' npm install @img/sharp-wasm32'
  69. );
  70. }
  71. if (isLinux && /(symbol not found|CXXABI_)/i.test(messages)) {
  72. try {
  73. const { config } = require(`@img/sharp-libvips-${runtimePlatform}/package`);
  74. const libcFound = `${familySync()} ${versionSync()}`;
  75. const libcRequires = `${config.musl ? 'musl' : 'glibc'} ${config.musl || config.glibc}`;
  76. help.push(
  77. '- Update your OS:',
  78. ` Found ${libcFound}`,
  79. ` Requires ${libcRequires}`
  80. );
  81. } catch (_errEngines) {}
  82. }
  83. if (isLinux && /\/snap\/core[0-9]{2}/.test(messages)) {
  84. help.push(
  85. '- Remove the Node.js Snap, which does not support native modules',
  86. ' snap remove node'
  87. );
  88. }
  89. if (isMacOs && /Incompatible library version/.test(messages)) {
  90. help.push(
  91. '- Update Homebrew:',
  92. ' brew update && brew upgrade vips'
  93. );
  94. }
  95. if (errors.some(err => err.code === 'ERR_DLOPEN_DISABLED')) {
  96. help.push('- Run Node.js without using the --no-addons flag');
  97. }
  98. // Link to installation docs
  99. if (isWindows && /The specified procedure could not be found/.test(messages)) {
  100. help.push(
  101. '- Using the canvas package on Windows?',
  102. ' See https://sharp.pixelplumbing.com/install#canvas-and-windows',
  103. '- Check for outdated versions of sharp in the dependency tree:',
  104. ' npm ls sharp'
  105. );
  106. }
  107. help.push(
  108. '- Consult the installation documentation:',
  109. ' See https://sharp.pixelplumbing.com/install'
  110. );
  111. throw new Error(help.join('\n'));
  112. }