To prevent the installation of a node package, it is recommended to ensure that the preinstall script returns a non-zero exit code.
Although you may still see npm ERR
messages, the npm process will not be terminated as it would be with the process.kill
method mentioned earlier, resulting in a proper npm log.
For instance, in the preinstall.js
file, you could include something like this:
if (someCondition) {
console.error('someCondition occurred, halting installation');
process.exit(1);
}
If someCondition
is met, it will display a message similar to this:
$ npm install ~/src/untracked/mypkg/mypkg-1.0.0.tgz
> [email protected] preinstall C:\Users\allon\src\git\samplenode\node_modules\mypkg
> node preinstall
someCondition occurred, halting installation
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] preinstall: `node preinstall`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] preinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/users/mureinik/.npm-cache/_logs/2020-11-29T09_58_46_179Z-debug.log
EDIT:
I am summarizing the conversation from the comments within the answer for easier reference if others encounter the same issue.
The objective here is to prevent the installation of a specific package without halting the entire npm install
process. While this behavior cannot be controlled by a preinstall script (which only affects the current package's installation success), it can be achieved by listing the dependency in the optionalDependencies
section of the package.json
.