Skip to content

Commit 0740043

Browse files
committedNov 6, 2023
Simplify logic to determine installation help
1 parent 8c5a493 commit 0740043

File tree

3 files changed

+66
-57
lines changed

3 files changed

+66
-57
lines changed
 

‎docs/install.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ When building your deployment package on a machine that differs from the target
118118
you will need to install either `@img/sharp-linux-x64` or `@img/sharp-linux-arm64` package.
119119

120120
```sh
121-
npm install --os=linux --cpu=x64
121+
npm install --os=linux --cpu=x64 sharp
122122
```
123123

124124
```sh
125-
npm install --os=linux --cpu=arm64
125+
npm install --os=linux --cpu=arm64 sharp
126126
```
127127

128128
When using npm 9 or earlier, this can be achieved using the following:

‎lib/sharp.js

+63-54
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,71 @@ const { familySync, versionSync } = require('detect-libc');
99

1010
const { runtimePlatformArch, prebuiltPlatforms, minimumLibvipsVersion } = require('./libvips');
1111
const runtimePlatform = runtimePlatformArch();
12-
const [isLinux, isMacOs, isWindows] = ['linux', 'darwin', 'win32'].map(os => runtimePlatform.startsWith(os));
1312

14-
/* istanbul ignore next */
15-
try {
16-
// Check for local build
17-
module.exports = require(`../src/build/Release/sharp-${runtimePlatform}.node`);
18-
} catch (errLocal) {
13+
const paths = [
14+
`../src/build/Release/sharp-${runtimePlatform}.node`,
15+
`@img/sharp-${runtimePlatform}/sharp.node`
16+
];
17+
18+
const errors = [];
19+
for (const path of paths) {
1920
try {
20-
// Check for runtime package
21-
module.exports = require(`@img/sharp-${runtimePlatform}/sharp.node`);
22-
} catch (errPackage) {
23-
const help = ['Could not load the "sharp" module at runtime'];
24-
if (errLocal.code !== 'MODULE_NOT_FOUND') {
25-
help.push(`${errLocal.code}: ${errLocal.message}`);
26-
}
27-
if (errPackage.code !== 'MODULE_NOT_FOUND') {
28-
help.push(`${errPackage.code}: ${errPackage.message}`);
29-
}
30-
help.push('Possible solutions:');
31-
// Common error messages
32-
if (prebuiltPlatforms.includes(runtimePlatform)) {
33-
const [os, cpu] = runtimePlatform.split('-');
34-
help.push('- Add an explicit dependency for the runtime platform:');
35-
help.push(` npm install --os=${os} --cpu=${cpu} sharp`);
36-
help.push(' or');
37-
help.push(` npm install --force @img/sharp-${runtimePlatform}`);
38-
} else {
39-
help.push(`- The ${runtimePlatform} platform requires manual installation of libvips >= ${minimumLibvipsVersion}`);
40-
}
41-
if (isLinux && /symbol not found/i.test(errPackage)) {
42-
try {
43-
const { engines } = require(`@img/sharp-libvips-${runtimePlatform}/package`);
44-
const libcFound = `${familySync()} ${versionSync()}`;
45-
const libcRequires = `${engines.musl ? 'musl' : 'glibc'} ${engines.musl || engines.glibc}`;
46-
help.push('- Update your OS:');
47-
help.push(` Found ${libcFound}`);
48-
help.push(` Requires ${libcRequires}`);
49-
} catch (errEngines) {}
50-
}
51-
if (isMacOs && /Incompatible library version/.test(errLocal.message)) {
52-
help.push('- Update Homebrew:');
53-
help.push(' brew update && brew upgrade vips');
54-
}
55-
if (errPackage.code === 'ERR_DLOPEN_DISABLED') {
56-
help.push('- Run Node.js without using the --no-addons flag');
57-
}
58-
if (process.versions.pnp) {
59-
help.push('- Use a supported yarn linker, either pnpm or node-modules:');
60-
help.push(' yarn config set nodeLinker node-modules');
61-
}
62-
// Link to installation docs
63-
if (isWindows && /The specified procedure could not be found/.test(errPackage.message)) {
64-
help.push('- Using the canvas package on Windows? See https://sharp.pixelplumbing.com/install#canvas-and-windows');
65-
} else {
66-
help.push('- Consult the installation documentation: https://sharp.pixelplumbing.com/install');
21+
module.exports = require(path);
22+
break;
23+
} catch (err) {
24+
/* istanbul ignore next */
25+
errors.push(err);
26+
}
27+
}
28+
29+
/* istanbul ignore next */
30+
if (!module.exports) {
31+
const [isLinux, isMacOs, isWindows] = ['linux', 'darwin', 'win32'].map(os => runtimePlatform.startsWith(os));
32+
33+
const help = [`Could not load the "sharp" module using the ${runtimePlatform} runtime`];
34+
errors.forEach(err => {
35+
if (err.code !== 'MODULE_NOT_FOUND') {
36+
help.push(`${err.code}: ${err.message}`);
6737
}
68-
throw new Error(help.join('\n'));
38+
});
39+
const messages = errors.map(err => err.message).join(' ');
40+
help.push('Possible solutions:');
41+
// Common error messages
42+
if (prebuiltPlatforms.includes(runtimePlatform)) {
43+
const [os, cpu] = runtimePlatform.split('-');
44+
help.push('- Add platform-specific dependencies:');
45+
help.push(` npm install --os=${os} --cpu=${cpu} sharp`);
46+
help.push(' or');
47+
help.push(` npm install --force @img/sharp-${runtimePlatform}`);
48+
} else {
49+
help.push(`- Manually install libvips >= ${minimumLibvipsVersion}`);
50+
}
51+
if (isLinux && /symbol not found/i.test(messages)) {
52+
try {
53+
const { engines } = require(`@img/sharp-libvips-${runtimePlatform}/package`);
54+
const libcFound = `${familySync()} ${versionSync()}`;
55+
const libcRequires = `${engines.musl ? 'musl' : 'glibc'} ${engines.musl || engines.glibc}`;
56+
help.push('- Update your OS:');
57+
help.push(` Found ${libcFound}`);
58+
help.push(` Requires ${libcRequires}`);
59+
} catch (errEngines) {}
60+
}
61+
if (isMacOs && /Incompatible library version/.test(messages)) {
62+
help.push('- Update Homebrew:');
63+
help.push(' brew update && brew upgrade vips');
64+
}
65+
if (errors.some(err => err.code === 'ERR_DLOPEN_DISABLED')) {
66+
help.push('- Run Node.js without using the --no-addons flag');
67+
}
68+
if (process.versions.pnp) {
69+
help.push('- Use a supported yarn linker, either pnpm or node-modules:');
70+
help.push(' yarn config set nodeLinker node-modules');
71+
}
72+
// Link to installation docs
73+
if (isWindows && /The specified procedure could not be found/.test(messages)) {
74+
help.push('- Using the canvas package on Windows? See https://sharp.pixelplumbing.com/install#canvas-and-windows');
75+
} else {
76+
help.push('- Consult the installation documentation: https://sharp.pixelplumbing.com/install');
6977
}
78+
throw new Error(help.join('\n'));
7079
}

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
"@img/sharp-libvips-win32-ia32": "0.0.3",
165165
"@img/sharp-libvips-win32-x64": "0.0.3",
166166
"@types/node": "*",
167-
"async": "^3.2.4",
167+
"async": "^3.2.5",
168168
"cc": "^3.0.1",
169169
"exif-reader": "^2.0.0",
170170
"extract-zip": "^2.0.1",

0 commit comments

Comments
 (0)
Please sign in to comment.