Whenever I run npm install <package>
it installs the package alright, but then it automatically runs the prepare
script.
It's worth mentioning that I've already checked that there is no postinstall
script in the package.json.
Whenever I run npm install <package>
it installs the package alright, but then it automatically runs the prepare
script.
It's worth mentioning that I've already checked that there is no postinstall
script in the package.json.
From https://docs.npmjs.com/misc/scripts:
prepare: Run both BEFORE the package is packed and published, and on local npm install without any arguments (See below). This is run AFTER prepublish, but BEFORE prepublishOnly.
Since NPM v5, prepare
script is executed when you run npm install
The other answers are fine, but for some additional context, this is to support a workflow where you can use devDependencies to build assets or other generated content for your project.
For example, say you want to use node-sass (CSS preprocessor). You add "node-sass" as a devDependency, then you run the sass command in your "prepare" script, which generates your CSS.
So, when you run npm install
, the following happens:
And when you run npm publish
, something similar happens:
So now when someone comes along and installs your package, they don't need node-sass or any of your devDependencies. They only need to runtime deps.
The prepare
script runs on local install and when installing git dependencies:
prepare: Run both BEFORE the package is packed and published, on local npm install without any arguments, and when installing git dependencies (See below). This is run AFTER prepublish, but BEFORE prepublishOnly.
You can avoid it with the --ignore-scripts
flag:
$ npm install <package> --ignore-scripts
From the doc https://docs.npmjs.com/misc/scripts
prepare: Run both BEFORE the package is packed and published, and on local npm install without any arguments (See below). This is run AFTER prepublish, but BEFORE prepublishOnly.
prepare
script run before publishing and after npm install
.
Now if you make an npm install
and one of the packages has a prepare
script, like for building, and it fails the whole install will fail.
We have two options:
npm install --ignore-scripts
That will run the ignore to all packages, which might be not the desired behavior. Imagine a third party package that needs to run prepare
and build
. If you run with --ignore-scripts
this will get skipped.
Add a package to the optionalDependencies
:
{
optionalDependencies: {
"myPackage": "^1.0.0"
}
}
If a dependency can be used, but you would like npm to proceed if it cannot be found or fails to install, then you may put it in the
optionalDependencies
object. This is a map of package name to version or url, just like thedependencies
object. The difference is that build failures do not cause installation to fail.
Entries in
optionalDependencies
will override entries of the same name independencies
, so it's usually best to only put in one place.
Check the doc:
https://docs.npmjs.com/cli/v7/configuring-npm/package-json#optionaldependencies
Note: With this, only the chosen package is concerned. And if it fails the installation will continue. That's usually what you want.
As per this answer in this thread:
https://github.com/npm/npm/issues/2817#issuecomment-368661749
the problem with --ignore-scripts is that is ignores all scripts. I just need to be able to ignore a script(s) for a particular package (the one where a build fails to compile on certain platforms). This option usually breaks my code because it has ignored ALL scripts in other packages that actually do need to run.
Anyway, to make this work like the OP I make the offending package optional. Then do a regular install, then a second install with --ignore-scripts. That way I get the scripts of other packages run first before ignoring them all (including the intended) the second time which then "fetches" the source of that package.
It's generally better to go with optionalDependencies
. That will most likely suit your needs.