diff --git a/CHANGELOG.md b/CHANGELOG.md index 18ac1528..718af19f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## main +- Improved error messaging when installing an incompatible npm version. - Added Node.js version 18.18.0. - Added Node.js version 20.7.0. diff --git a/lib/binaries.sh b/lib/binaries.sh index d54ec3d3..c2c2fba4 100644 --- a/lib/binaries.sh +++ b/lib/binaries.sh @@ -123,8 +123,10 @@ install_npm() { echo "npm $npm_version already installed with node" else echo "Bootstrapping npm $version (replacing $npm_version)..." - if ! npm install --unsafe-perm --quiet -g "npm@$version" 2>@1>/dev/null; then - echo "Unable to install npm $version; does it exist?" && false + if ! npm install --unsafe-perm --quiet --no-audit --no-progress -g "npm@$version" >/dev/null; then + echo "Unable to install npm $version. " \ + "Does npm $version exist? " \ + "Is npm $version compatible with this Node.js version?" && false fi # Verify npm works before capturing and ensure its stderr is inspectable later npm --version 2>&1 1>/dev/null diff --git a/test/fixtures/npm-version-incompat/README.md b/test/fixtures/npm-version-incompat/README.md new file mode 100644 index 00000000..cda334ae --- /dev/null +++ b/test/fixtures/npm-version-incompat/README.md @@ -0,0 +1 @@ +A fake README, to keep npm from polluting stderr. \ No newline at end of file diff --git a/test/fixtures/npm-version-incompat/package.json b/test/fixtures/npm-version-incompat/package.json new file mode 100644 index 00000000..d3a1bd7f --- /dev/null +++ b/test/fixtures/npm-version-incompat/package.json @@ -0,0 +1,13 @@ +{ + "name": "node-buildpack-test-app", + "version": "0.0.1", + "description": "node buildpack integration test app", + "repository" : { + "type" : "git", + "url" : "http://github.com/example/example.git" + }, + "engines": { + "node": "16.x", + "npm": "10.x" + } +} diff --git a/test/run b/test/run index f22cbd0a..025331a3 100755 --- a/test/run +++ b/test/run @@ -840,6 +840,13 @@ testNpmVersionSpecific() { assertCapturedSuccess } +testNpmVersionIncompatible() { + compile "npm-version-incompat" + assertCaptured "Unable to install npm 10.x" + assertCaptured "Is npm 10.x compatible with this Node.js version?" + assertCapturedError +} + testFailingBuild() { compile "failing-build" assertCaptured "Build failed" @@ -1139,7 +1146,7 @@ testNodeEnvNpmConfigProductionFalse() { assertCaptured "preinstall: production" assertCaptured "postinstall: production" assertCaptured "heroku-postbuild: production" - assertCapturedSuccess + assertEquals "Expected successful exit code" "0" "${RETURN}" } # Avoid this issue