I have npm module with following package.json
{
"name": "my-app",
"version": "0.0.0",
"scripts": {
"prepublish": "bower install",
"build": "gulp"
},
"dependencies": {
"express": "~4.0.0",
"body-parser": "~1.0.1"
},
"devDependencies": {
"gulp": "~3.6.0",
"bower": "~1.3.2"
}
}
When I deploy my app to production I don't want install devDependecies so, I run npm install --production
. But in this case prepublish
script are called, but it doesn't need, because I use CDN links in production.
How to call postinstall script only after npm install
but not after npm install --production
?
I think you cannot choose what scripts are run based on the
--production
argument. What you can do, however, is supply a script which tests theNODE_ENV
variable and only runsbower install
if it's not "production".If you are always in a unix-y environment, you can do it like this:
I'm using if-env module. It's less verbose.
PS: I didn't test it on windows yet.
Install with:
than in
package.json
scripts:I have a more general problem - where I want to skip running the
postinstall
script on local (direct) installs - like when I'm developing the package and runyarn add --dev my-new-dependency
.This is what I came up with. It works with both npm and yarn.
postinstall.js:
package.json:
This only works if you're on a unix-like environment:
NPM sets an environment variable to "true" when install is run with --production. To only run the postinstall script if npm install was not run with --production, use the following code.
Solution that is less dependent on unix nature of your shell:
Newer npm (& Yarn) versions include support for the
prepare
script that is run after eachinstall
run but only in development mode. Also, theprepublish
is deprecated. This should be enough:Docs: https://docs.npmjs.com/misc/scripts