-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
sharp.js
69 lines (64 loc) · 3.04 KB
/
sharp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2013 Lovell Fuller and others.
// SPDX-License-Identifier: Apache-2.0
'use strict';
// Inspects the runtime environment and exports the relevant sharp.node binary
const { familySync, versionSync } = require('detect-libc');
const { runtimePlatformArch, prebuiltPlatforms, minimumLibvipsVersion } = require('./libvips');
const runtimePlatform = runtimePlatformArch();
const [isLinux, isMacOs, isWindows] = ['linux', 'darwin', 'win32'].map(os => runtimePlatform.startsWith(os));
/* istanbul ignore next */
try {
// Check for local build
module.exports = require(`../src/build/Release/sharp-${runtimePlatform}.node`);
} catch (errLocal) {
try {
// Check for runtime package
module.exports = require(`@img/sharp-${runtimePlatform}/sharp.node`);
} catch (errPackage) {
const help = ['Could not load the "sharp" module at runtime'];
if (errLocal.code !== 'MODULE_NOT_FOUND') {
help.push(`${errLocal.code}: ${errLocal.message}`);
}
if (errPackage.code !== 'MODULE_NOT_FOUND') {
help.push(`${errPackage.code}: ${errPackage.message}`);
}
help.push('Possible solutions:');
// Common error messages
if (prebuiltPlatforms.includes(runtimePlatform)) {
help.push('- Add an explicit dependency for the runtime platform:');
help.push(` npm install --force @img/sharp-${runtimePlatform}`);
} else {
help.push(`- The ${runtimePlatform} platform requires manual installation of libvips >= ${minimumLibvipsVersion}`);
}
if (isLinux && /symbol not found/i.test(errPackage)) {
try {
const { engines } = require(`@img/sharp-libvips-${runtimePlatform}/package`);
const libcFound = `${familySync()} ${versionSync()}`;
const libcRequires = `${engines.musl ? 'musl' : 'glibc'} ${engines.musl || engines.glibc}`;
help.push('- Update your OS:');
help.push(` Found ${libcFound}`);
help.push(` Requires ${libcRequires}`);
} catch (errEngines) {}
}
if (isMacOs && /Incompatible library version/.test(errLocal.message)) {
help.push('- Update Homebrew:');
help.push(' brew update && brew upgrade vips');
}
if (errPackage.code === 'ERR_DLOPEN_DISABLED') {
help.push('- Run Node.js without using the --no-addons flag');
}
if (process.versions.pnp) {
help.push('- Use a supported yarn linker, either pnpm or node-modules:');
help.push(' yarn config set nodeLinker node-modules');
}
// Link to installation docs
if (isLinux && /Module did not self-register/.test(errLocal.message + errPackage.message)) {
help.push('- Using worker threads on Linux? See https://sharp.pixelplumbing.com/install#worker-threads');
} else if (isWindows && /The specified procedure could not be found/.test(errPackage.message)) {
help.push('- Using the canvas package on Windows? See https://sharp.pixelplumbing.com/install#canvas-and-windows');
} else {
help.push('- Consult the installation documentation: https://sharp.pixelplumbing.com/install');
}
throw new Error(help.join('\n'));
}
}