From 516b1ec332c32b263ec832d9051dd09466b88399 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Mon, 11 Dec 2023 21:28:09 +0000 Subject: [PATCH] Improve help text displayed on failure of require --- lib/sharp.js | 57 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/lib/sharp.js b/lib/sharp.js index 74496c52..c002f844 100644 --- a/lib/sharp.js +++ b/lib/sharp.js @@ -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')); }