Zhao Yi Zhao Yi - 2 months ago 11x
Node.js Question

How to define scripts for npm command?

I am using npm+webpack to manage a react web application. And I want to define a few command for npm in order to run some webpack command. Below is the two command scripts I defined in package.json file.

"scripts": {
"start": "webpack-dev-server --host",
"build": "NODE_ENV=production webpack --config ./webpack.config.js --progress --profile --colors"

As you can see, there are two commands 'start' and 'build'. When I run
npm start
it will run
webpack-dev-server --host
to launch a web server.
But when I run
npm build
, it doesn't run the configured command and simply returns without any output. I wander how to define a script command for npm to use. Below is my whole package.json file:


"name": "demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --host",
"build": "NODE_ENV=production webpack --config ./webpack.config.js --progress --profile --colors"
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.9.0",
"babel-loader": "^6.2.4",
"compression-webpack-plugin": "^0.3.1",
"css-loader": "^0.23.1",
"less": "^2.7.1",
"less-loader": "^2.2.3",
"npm-install-webpack-plugin": "^3.1.3",
"style-loader": "^0.13.1",
"svg-sprite-loader": "0.0.26",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1",
"webpack-shell-plugin": "^0.4.2"
"dependencies": {
"actions": "^1.3.0",
"axios": "^0.12.0",
"babel-preset-stage-2": "^6.11.0",
"cdnjs": "^0.3.2",
"components": "^0.1.0",
"containers": "0.0.1",
"extract-text-webpack-plugin": "^1.0.1",
"features": "^0.1.0",
"file-loader": "^0.8.5",
"fo": "^0.1.1",
"jshint": "^2.9.2",
"jshint-loader": "^0.8.3",
"leaflet": "^0.7.7",
"material-ui": "^0.15.2",
"moment": "^2.13.0",
"normalize.css": "^3.0.2",
"nuka-carousel": "^2.0.0",
"public": "^0.1.2",
"query-string": "^4.2.2",
"react": "^15.1.0",
"react-addons-css-transition-group": "^15.1.0",
"react-addons-shallow-compare": "^15.1.0",
"react-alert": "^1.0.14",
"react-button": "^1.2.1",
"react-cookie": "^0.4.7",
"react-date-picker": "^5.3.9",
"react-datepicker": "^0.27.0",
"react-dom": "^15.0.2",
"react-infinite-calendar": "^1.1.14",
"react-redux": "^4.4.5",
"react-router": "^2.4.1",
"react-router-redux": "^4.0.5",
"react-select": "^1.0.0-beta13",
"react-spinkit": "^1.1.8",
"react-tap-event-plugin": "^1.0.0",
"react-tappable": "^0.8.1",
"redux": "^3.5.2",
"redux-thunk": "^2.1.0",
"sha1": "^1.1.1",
"source-map-loader": "^0.1.5",
"src": "^1.1.2",
"style": "0.0.3",
"url-loader": "^0.5.7",
"utils": "^0.3.1"


You can use npm run-script <script-name> to run arbitrary scripts. In your case, npm run-script build.

From the npm documentation, The npm <script-name> syntax is only supported for a specific number of predetermined scripts such as start.

npm supports the "scripts" property of the package.json script, for the following scripts:

  • prepublish: Run BEFORE the package is published. (Also run on local npm install without any arguments.)
  • publish, postpublish: Run AFTER the package is published.
  • preinstall: Run BEFORE the package is installed
  • install, postinstall: Run AFTER the package is installed.
  • preuninstall, uninstall: Run BEFORE the package is uninstalled.
  • postuninstall: Run AFTER the package is uninstalled.
  • preversion, version: Run BEFORE bump the package version.
  • postversion: Run AFTER bump the package version.
  • pretest, test, posttest: Run by the npm test command.
  • prestop, stop, poststop: Run by the npm stop command.
  • prestart, start, poststart: Run by the npm start command.
  • prerestart, restart, postrestart: Run by the npm restart command. Note: npm restart will run the stop and start scripts if no restart script is provided.

Additionally, arbitrary scripts can be executed by running npm run-script <pkg> <stage>. Pre and post commands with matching names will be run for those as well (e.g. premyscript, myscript, postmyscript).

As well, you can use npm run <script-name> as a shorthand.