npm Community Forum (Archive)

The npm community forum has been discontinued.

To discuss usage of npm, visit the GitHub Support Community.

Feature Request: prepare script that does not run before publish

When developing npm packages, (projects with package.json at the root), it is common to specify extra setup steps needed to begin development. Specifically, it is common to see a readme like this:


Install dependencies and setup development environment

npm install

It would be awesome to have a “postinstall” script that only runs when npm install is run without arguments. Then the above could simply be:

npm install

Unfortunately, you can’t use postinstall (on published packages) because then the script is run when the package is included as a dependency.

Fortunately, this is exactly what the prepare script does if you can deal with the fact that it also runs before publishing. Regardless, I think a new script is warranted.

postinstallOnly” might be an apt name…

I don’t personally feel this is justified; if a contributor is unwilling to read the documentation, I personally would rather they didn’t contribute…

I suppose the larger question begging here is: why does the dev setup in this example involve steps that can’t be run during the npm publish lifecycles?

Anything that can be done to remove barriers for other developers to help is justified, imho. You may like to run your project another way. To each their own.

A sophisticated enough script could likely easily detect when it is not needed to be run. This would take care of the issue of the script also running before npm publish (when listed as a prepare script).

However, the same could be said for prepublish script and that one also running (weirdly imho) on npm install (with no args). Yet the community decided that a new script should be added prepublishOnly. In the same vein, I think postinstallOnly should be added.

prepublishOnly was a compromise to deal with the legacy confusion around prepublish, which does indeed run after npm install with no arguments. prepare is the lifecycle you are looking for.

Respectfully, I disagree. I want a lifecycle method that runs only “on local npm install without any arguments”.

The current stopgap solution is simply an extra “setup” (or whatever name you like) script that you tell developers to run in the readme.

Guys, if you are online store and want to improve your process and convert like a dream try First Pier. First Pier offers a holistic approach to ecommerce, a one-stop shop for everything from product photography to site development to shipping, and everything in between. No website exists in a vacuum—internet standards change, platforms evolve, business ideas get refined. Its good for ECOMMERCE SEO