Marcus Junius Brutus Marcus Junius Brutus - 2 months ago 12
Node.js Question

running 'npm install' from shell also runs the 'prepublish' script

This is a SSCCE.
Given the following

package.json
file:

{
"name": "foo",
"version": "1.0.0",
"description": "",
"scripts": {
"prepublish": "echo \"pre-publish script called\""
},
"devDependencies": {
},
"dependencies": {
"lodash": "^4.10.0"
}
}


Running:
npm install
from the shell, results in the
prepublish
script also being executed:

$ npm install

> foo@1.0.0 prepublish /tmp/so
> echo "pre-publish script called"

pre-publish script called
npm WARN foo@1.0.0 No description
npm WARN foo@1.0.0 No repository field.
npm WARN foo@1.0.0 No license field.


If the
prepublish
script gets renamed to e.g.
prepublis
it is no longer executed with
npm install
. Problem is, I need the
prepublish
script as I typically perform static type analysis and Mocha tests prior to publishing to npm.

Is this a bug or a feature and how do I get around it? There should be no need to run my Mocha tests when I simply wish to install the
package.json
dependencies.

My environment is:

$ npm --version && node --version
3.9.5
v6.2.2

Answer

As I said it's not a bug (although everyone wants this functionality to be fixed), but there is a solution. Check out https://www.npmjs.com/package/in-publish to help with this very situation.

Solution summary

(from the in-publish package page)

 npm install --save in-publish

Then in package.json:

  "scripts": {
    "prepublish": "in-publish && thing-I-dont-want-on-dev-install || not-in-publish"
  }

You can also use not-in-install and in-install instead of in-publish

  "scripts": {
    "prepublish": "not-in-install && thing-I-dont-want-on-dev-install || in-install"
  }
Comments