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