Improve help text displayed on failure of require

This commit is contained in:
Lovell Fuller 2023-12-11 21:28:09 +00:00
parent 95ba045a69
commit 516b1ec332

View File

@ -46,43 +46,58 @@ if (sharp) {
// Common error messages
if (prebuiltPlatforms.includes(runtimePlatform)) {
const [os, cpu] = runtimePlatform.split('-');
help.push('- Ensure optional dependencies can be installed:');
help.push(' npm install --include=optional');
help.push('- Add platform-specific dependencies:');
help.push(` npm install --os=${os} --cpu=${cpu} sharp`);
help.push(' or');
help.push(` npm install --force @img/sharp-${runtimePlatform}`);
help.push(
'- Ensure optional dependencies can be installed:',
' npm install --include=optional sharp',
' or',
' yarn add sharp --ignore-engines',
'- Add platform-specific dependencies:',
` npm install --os=${os} --cpu=${cpu} sharp`,
' or',
` npm install --force @img/sharp-${runtimePlatform}`
);
} else {
help.push(`- Manually install libvips >= ${minimumLibvipsVersion}`);
help.push('- Add experimental WebAssembly-based dependencies:');
help.push(' npm install --cpu=wasm32 sharp');
help.push(
`- Manually install libvips >= ${minimumLibvipsVersion}`,
'- Add experimental WebAssembly-based dependencies:',
' npm install --cpu=wasm32 sharp',
' or',
' npm install --force @img/sharp-wasm32'
);
}
if (isLinux && /symbol not found/i.test(messages)) {
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}`);
help.push(
'- Update your OS:',
` Found ${libcFound}`,
` Requires ${libcRequires}`
);
} catch (errEngines) {}
}
if (isMacOs && /Incompatible library version/.test(messages)) {
help.push('- Update Homebrew:');
help.push(' brew update && brew upgrade vips');
help.push(
'- Update Homebrew:',
' brew update && brew upgrade vips'
);
}
if (errors.some(err => err.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 (isWindows && /The specified procedure could not be found/.test(messages)) {
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');
help.push(
'- Using the canvas package on Windows?',
' See https://sharp.pixelplumbing.com/install#canvas-and-windows',
'- Check for outdated versions of sharp in the dependency tree:',
' npm ls sharp'
);
}
help.push(
'- Consult the installation documentation:',
' https://sharp.pixelplumbing.com/install'
);
throw new Error(help.join('\n'));
}