Difference with NPM aliases and commands

First times automating NPM and using scripts in package.json can be confusing. Usual syntax shown is npm start and when you create your own script with some other name it doesn't work. Or worse, it does something totally different.

Aliases

Worry not. NPM scripts are used to use with command npm run-script <script-name>. Confusing is that there is some commands which are aliases for scripts:

  • install runs prepublish, publish, preinstall & install scripts
  • test runs pretest, test & posttest
  • stop runs prestop, stop & poststop
  • start runs prestart, start & poststart
  • restart runs prerestart, restart, postrestart

Mentioned scripts can be overriden. Which I propose if it works with your apps life cycle.

Custom scripts

As noticed, it's much safer still to run npm run-script <script-name> to use the script. You can't run custom script straight with npm <script-name>.

Notice that if you run script, also pre and post scripts are run if exist. Pre- and postscripts are named with your script name with prefix. Example if your script name is diamond:

prediamond, diamond and postdiamond are run.