npm scripts
Syntax#
- The “scripts” property in
package.json
allows you to run npm packages locally. - The
"karma": "karma"
script references thekarma
shell script is thenode_modules/.bin
directory. This reference needs to be grab and an alias needs to be applied to it in order to be used in other npm scripts, such as"test": "karma start"
.
Remarks#
Pre-recognized scripts
prepublish
: Run before the package is publishedpublish
,postpublish
: Run after the package is publishedpreinstall
: Run before the package is installedinstall
,postinstall
: Run after the package is installedpreversion
,version
: Run before bump the package versionpostversion
: Run after bump the package versionpretest
,test
,posttest
: Run by thenpm test
commandprestop
,stop
,poststop
: Run by thenpm stop
commandprestart
,start
,poststart
: Run by thenpm start
commandprerestart
,restart
,postrestart
: Run by thenpm restart
command. Note:npm restart
will run the stop and start scripts if norestart
script is provided.
It can be deduced that the "scripts"
property in package.json
is a very powerful tool. It can be used as a build tool, similar to the likes of Grunt and Gulp, but with over 250,000 packages available. NPM scripts runs npm packages installed locally to your project from the node_modules/.bin
directory.
Running karma locally
package.json
snippet
{
"scripts":
"test": "karma start",
"karma": "karma"
}
}
Running npm scripts
There are two types of npm scripts, and the command to run each is slightly different. The first type of npm scripts are “pre-recognized” scripts. These scripts are automatically recognized by npm and don’t need a special prefix (as you will see for the other type) to run them. The other type of scripts are “custom” scripts. These scripts aren’t pre-recognized by npm and have to be prefixed by a special command to run them. There are a list of pre-recognized scripts in the remarks section.
To run pre-recognized scripts:
npm start
or npm test
To run custom scripts you need to use the run
command:
npm run karma
What npm scripts are and how are they triggered
The npm scripts are commands that npm
will run for you when called with the proper arguments. The power and sense of this is to NOT install the npm packages globally poluting your environment.
The difference between pre-recognized and custom scripts relies on the run
word between the tags, custom
scripts will need the run
between npm and the script name
Based on this we can differenciate and create different tasks or scripts to be run with npm.
Given the following example on the package.json
file:
{
"name": "MyApp",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "mocha --recursive ./tests/",
"test:watch": "npm run test -- -w",
"start": "nodemon --inspect ./app.js",
"build": "rm -rf ./dist/ && gulp build"
}
...
}
We can see different tasks to be run:
-
npm test
Would work fine as it is a pre-recognized script -
npm run test
Would work fine as it is a valid way to execute a npm script -
npm run test:watch
Would work also, and it’s calling npm run test inside itself -
npm run build
Would before runninggulp build
delete thedist
folder that is in the directory (assuming you are in Linux or the commandrm
is recognized)