node.js - 如何为 npm 命令定义脚本?

标签 node.js webpack

我正在使用 npm+webpack 来管理 React 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”和“build”。当我运行 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"
  }
}

最佳答案

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

来自npm文档,npm <script-name>仅特定数量的预定脚本支持语法,例如 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).

您也可以使用 npm run <script-name>作为简写。

关于node.js - 如何为 npm 命令定义脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38863971/

相关文章:

node.js - 您可以设置多少个 setTimeouts?

node.js - 发送 Content-Type : application/json post with node. js

javascript - `npm build` 没有在 package.json 中构建 't run the script named "

javascript - react : 404 Found when importing local images

angular - 将 Angular 13 用于 Electron 应用程序时如何升级 webpack 5?

javascript - 为什么在将 webpack 与 babel 一起使用时,使用命名导出导入默认导出会导致错误?

json - Vue/Vuex - 从 JSON 文件加载状态

javascript - 无法将虚拟键盘 jQuery 插件与 Angular 4 +Webpack 一起使用

node.js - Netlify部署: 12:43:01 AM: Build failed due to a user error: Build script returned non-zero exit code: 2

javascript - 如何追加到 Node 中的文件?