如何为npm命令定义脚本?

我正在使用npm + webpack来pipe理反应的Web应用程序。 我想为npm定义一些命令来运行一些webpack命令。 下面是我在package.json文件中定义的两个命令脚本。

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

正如你所看到的,有两个命令“开始”和“build立”。 当我运行npm start ,它将运行webpack-dev-server --host 0.0.0.0来启动一个web服务器。 但是当我运行npm build ,它不运行configuration的命令,只是简单地返回没有任何输出。 我徘徊如何定义一个脚本命令为npm使用。 下面是我的整个package.json文件:

 { "name": "demo", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "start": "webpack-dev-server --host 0.0.0.0", "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" } } 

您可以使用npm run-script <script-name>来运行任意脚本。 在你的情况下, npm run-script build

npm文档中, npm <script-name>语法仅支持特定数量的预定脚本(如start

npm支持package.json脚本的“scripts”属性,用于以下脚本:

  • 预发布:在包发布之前运行。 (也可以在没有任何参数的情况下在本地npm上运行。)
  • 发布,发布后:在包发布后运行。
  • 预安装:在安装软件包之前运行
  • 安装,安装后:在安装软件包后运行。
  • preuninstall,卸载:在卸载软件包之前运行。
  • postuninstall:卸载软件包后运行。
  • 反转,版本:运行之前撞包的版本。
  • 后期版本:运行后冲击包版本。
  • 预testing,testing,后测:由npm test命令运行。
  • prestop,stop,poststop:由npm stop命令运行。
  • prestart,start,poststart:由npm start命令运行。
  • prerestart,restart,postrestart:由npm restart命令运行。 注意:如果没有提供重启脚本,npm restart将运行stop和start脚本。

另外, 可以通过运行npm run-script <pkg> <stage>来执行任意脚本 。 前后命令匹配的名字也将运行这些(例如premyscript,myscript,postmyscript)。

另外,可以使用npm run <script-name>作为简写。

你可以像在这里一样定义一个npm脚本:

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

要运行构build命令,请input: $ npm run build

你需要使用命令npm run build