如何为 npm 命令定义脚本? [英] How to define scripts for npm command?

查看:33
本文介绍了如何为 npm 命令定义脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 0.0.0.0",
"build": "NODE_ENV=production webpack --config ./webpack.config.js --progress --profile --colors"
},

如您所见,有两个命令start"和build".当我运行 npm start 时,它会运行 webpack-dev-server --host 0.0.0.0 来启动一个网络服务器.但是当我运行 npm build 时,它不会运行配置的命令,只是返回而没有任何输出.我想知道如何定义一个供 npm 使用的脚本命令.下面是我的整个 package.json 文件:

As you can see, there are two commands 'start' and 'build'. When I run npm start it will run webpack-dev-server --host 0.0.0.0 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 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 来运行任意脚本.在您的情况下,npm run-script build.

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

npm 文档来看,npm 语法仅支持特定数量的预定脚本,例如 start.

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

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

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

  • prepublish:在发布包之前运行.(也可以在没有任何参数的情况下在本地 npm install 上运行.)
  • publish、postpublish:在发布包后运行.
  • 预安装:在安装包之前运行
  • 安装、安装后:安装包后运行.
  • 预卸载、卸载:在卸载包之前运行.
  • postuninstall:在卸载软件包后运行.
  • preversion、version:在修改包版本之前运行.
  • postversion:在修改包版本后运行.
  • 预测试、测试、后测试:通过 npm test 命令运行.
  • prestop、stop、poststop:通过 npm stop 命令运行.
  • prestart、start、poststart:通过 npm start 命令运行.
  • prerestart、restart、postrestart:通过 npm restart 命令运行.注意:如果没有提供重启脚本,npm restart 将运行停止和启动脚本.

另外,任意脚本可以通过运行npm run-script <pkg><阶段>.具有匹配名称的前置和后置命令也将针对那些运行(例如 premyscript、myscript、postmyscript).

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).

同样,您可以使用 npm run 作为速记.

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

这篇关于如何为 npm 命令定义脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆