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 // Common error messages
if (prebuiltPlatforms.includes(runtimePlatform)) { if (prebuiltPlatforms.includes(runtimePlatform)) {
const [os, cpu] = runtimePlatform.split('-'); const [os, cpu] = runtimePlatform.split('-');
help.push('- Ensure optional dependencies can be installed:'); help.push(
help.push(' npm install --include=optional'); '- Ensure optional dependencies can be installed:',
help.push('- Add platform-specific dependencies:'); ' npm install --include=optional sharp',
help.push(` npm install --os=${os} --cpu=${cpu} sharp`); ' or',
help.push(' or'); ' yarn add sharp --ignore-engines',
help.push(` npm install --force @img/sharp-${runtimePlatform}`); '- Add platform-specific dependencies:',
` npm install --os=${os} --cpu=${cpu} sharp`,
' or',
` npm install --force @img/sharp-${runtimePlatform}`
);
} else { } else {
help.push(`- Manually install libvips >= ${minimumLibvipsVersion}`); help.push(
help.push('- Add experimental WebAssembly-based dependencies:'); `- Manually install libvips >= ${minimumLibvipsVersion}`,
help.push(' npm install --cpu=wasm32 sharp'); '- 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)) { if (isLinux && /symbol not found/i.test(messages)) {
try { try {
const { engines } = require(`@img/sharp-libvips-${runtimePlatform}/package`); const { engines } = require(`@img/sharp-libvips-${runtimePlatform}/package`);
const libcFound = `${familySync()} ${versionSync()}`; const libcFound = `${familySync()} ${versionSync()}`;
const libcRequires = `${engines.musl ? 'musl' : 'glibc'} ${engines.musl || engines.glibc}`; const libcRequires = `${engines.musl ? 'musl' : 'glibc'} ${engines.musl || engines.glibc}`;
help.push('- Update your OS:'); help.push(
help.push(` Found ${libcFound}`); '- Update your OS:',
help.push(` Requires ${libcRequires}`); ` Found ${libcFound}`,
` Requires ${libcRequires}`
);
} catch (errEngines) {} } catch (errEngines) {}
} }
if (isMacOs && /Incompatible library version/.test(messages)) { if (isMacOs && /Incompatible library version/.test(messages)) {
help.push('- Update Homebrew:'); help.push(
help.push(' brew update && brew upgrade vips'); '- Update Homebrew:',
' brew update && brew upgrade vips'
);
} }
if (errors.some(err => err.code === 'ERR_DLOPEN_DISABLED')) { if (errors.some(err => err.code === 'ERR_DLOPEN_DISABLED')) {
help.push('- Run Node.js without using the --no-addons flag'); 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 // Link to installation docs
if (isWindows && /The specified procedure could not be found/.test(messages)) { 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'); help.push(
} else { '- Using the canvas package on Windows?',
help.push('- Consult the installation documentation: https://sharp.pixelplumbing.com/install'); ' 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')); throw new Error(help.join('\n'));
} }