我正在使用npm + webpack来管理反应式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"
},
正如您所看到的,有两个命令' start'并且'建立'。当我运行npm start
时,它将运行webpack-dev-server --host 0.0.0.0
以启动Web服务器。
但是当我运行npm build
时,它不会运行配置的命令而只返回没有任何输出。我徘徊如何定义一个脚本命令供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"
}
}
答案 0 :(得分:1)
您可以使用npm run-script <script-name>
运行任意脚本。在您的情况下,npm run-script build
。
在npm
文档中,npm <script-name>
语法仅支持特定数量的预定脚本,例如start
。
npm支持package.json脚本的“scripts”属性 以下脚本:
- prepublish:在发布包之前运行。 (也可以在没有任何参数的本地npm安装上运行。)
- 发布,发布后:在发布包之后运行。
- 预安装:在安装软件包之前运行
- install,postinstall:安装软件包后运行。
- 预安装,卸载:在卸载软件包之前运行。
- postuninstall:在卸载软件包后运行。
- preversion,version:在碰撞包版本之前运行。
- postversion:运行AFTER碰撞包版本。
- pretest,test,posttest:由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>
来执行任意脚本。具有匹配名称的前置和后置命令 也将为那些人运行(例如,前提,myscript, postmyscript)。
同样,您可以使用npm run <script-name>
作为速记。
答案 1 :(得分:0)
您可以像在此处一样定义npm脚本:
"scripts": {
"start": "webpack-dev-server --host 0.0.0.0",
"build": "NODE_ENV=production webpack --config ./webpack.config.js --progress --profile --colors"
},
要运行build命令,请输入:
$ npm run build
答案 2 :(得分:0)
您需要使用命令npm run build