Running scripts
suggest changeYou may define scripts in your package.json, for example:
{
"name": "your-package",
"version": "1.0.0",
"description": "",
"main": "index.js",
"author": "",
"license": "ISC",
"dependencies": {},
"devDependencies": {},
"scripts": {
"echo": "echo hello!"
}
}
To run the echo script, run npm run echo from the command line. Arbitrary scripts, such as echo above, have to be run with npm run <script name>. npm also runs a set of named scripts at certain stages of the package life cycle (like preinstall). See the scripts documentation.
npm scripts are used most often for things like starting a server, building the project, and running tests. Here’s a more realistic example:
"scripts": {
"test": "mocha tests",
"start": "pm2 start index.js"
}
npm puts node_modules/.bin at the front of PATH for scripts, so a locally installed mocha is found before a global one.
If your scripts become very long, they can be split into parts, like this:
"scripts": {
"very-complex-command": "npm run chain-1 && npm run chain-2",
"chain-1": "webpack",
"chain-2": "node app.js"
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents