Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is npm running prepare script after npm install, and how can I stop it?

Tags:

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.

like image 360
Alex Zak Avatar asked Jun 12 '17 12:06

Alex Zak


People also ask

How do I stop npm from running?

To stop a running npm process, press CTRL + C or close the shell window.

How do I resolve npm installation issues?

The easiest way to fix the issue is to pass an additional parameter –legacy-peer-deps to npm install. The --legacy-peer-deps tells the npm to ignore the peer dependencies and continue the installation of the package. Try the below command to install the dependencies for your project.

What is prepare npm?

prepare (since [email protected] ) Runs any time before the package is packed, i.e. during npm publish and npm pack. Runs BEFORE the package is packed. Runs BEFORE the package is published. Runs on local npm install without any arguments.

How do I start and stop npm?

Use the npm start to start a package. As of the [email protected], you can make use of custom arguments when executing scripts. This command is used to stop a package. This command will run the stop script that is provided by the package.


2 Answers

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

like image 98
Yasha Avatar answered Oct 08 '22 20:10

Yasha


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:

  • dependencies and devDependencies get installed
  • your "prepare" script generates your CSS
  • your project is ready to go with all necessary CSS

And when you run npm publish, something similar happens:

  • your "prepare" script generates your CSS
  • your code and generated CSS are published to the npm repo

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.

like image 35
Rich Remer Avatar answered Oct 08 '22 20:10

Rich Remer