Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Npm postinstall only on development

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 is called, but it doesn't need to, because I use CDN links in production.

How to call postinstall script only after npm install but not after npm install --production?

like image 469
just-boris Avatar asked Apr 15 '14 07:04

just-boris


People also ask

Does npm CI run Postinstall?

command. Not only does it build native addons, but it also runs preinstall, install, and postinstall scripts.

What is Postinstall?

The postinstall script creates the backout package using the information provided by the other scripts. Since the pkgmk and pkgtrans commands do not require the package database, they can be executed within a package installation.

How npm scripts work?

An npm script is a convenient way to bundle common shell commands for your project. They are typically commands, or a string of commands, which would normally be entered at the command line in order to do something with your application. Scripts are stored in a project's package.


3 Answers

Newer npm (& Yarn) versions include support for the prepare script that is run after each install run but only in development mode. Also, the prepublish is deprecated. This should be enough:

{
  scripts: {
    "prepare": "bower install"
  }
}

Docs: https://docs.npmjs.com/misc/scripts

like image 128
mgol Avatar answered Oct 28 '22 06:10

mgol


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 the NODE_ENV variable and only runs bower install if it's not "production".

If you are always in a unix-y environment, you can do it like this:

{ 
  scripts: {
    "prepublish": "[ \"$NODE_ENV\" = production ] && exit 0; bower install"
  }
}
like image 44
Sam Mikes Avatar answered Oct 28 '22 04:10

Sam Mikes


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.

"postinstall": "if [ -z \"$npm_config_production\" ]; then node_modules/gulp/bin/gulp.js first-run; fi",
like image 17
Synthe Avatar answered Oct 28 '22 04:10

Synthe