无法用'npm run build'替换命令'webpack'

时间:2018-03-27 14:16:19

标签: javascript node.js npm webpack npm-scripts

请别人向我解释一下。 我想使用命令npm run build来调用webpack。所以我修改了文件package.json中的脚本。但它没有用。我正在使用linux

这是我的package.json文件

{
  "name": "learn-webpack",
  "version": "1.0.0",
  "description": "Learn Webpack",
  "main": "index.js",
  "scripts": {
    "build": "webpack"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.6.1",
    "css-loader": "^0.28.11",
    "style-loader": "^0.20.3",
    "webpack": "^4.2.0"
  }
}

这是错误,它已经安装了cli但它会继续发送此消息

The CLI moved into a separate package: webpack-cli.
Please install 'webpack-cli' in addition to webpack itself to use the CLI.
-> When using npm: npm install webpack-cli -D
-> When using yarn: yarn add webpack-cli -D
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! learn-webpack@1.0.0 build: `webpack`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the learn-webpack@1.0.0 build script.
npm ERR! This is probably not a problem with npm.
         There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/huong/.npm/_logs/2018-03-27T14_09_22_103Z-debug.log

4 个答案:

答案 0 :(得分:3)

按照说明

  

The CLI moved into a separate package: webpack-cli.

     

Please install 'webpack-cli' in addition to webpack itself to use the CLI.

     

-> When using npm: npm install webpack-cli -D

跑步:

npm install webpack-cli -D -g

-g为了更好的衡量标准,然后尝试再次运行构建,它应该可以工作:)

答案 1 :(得分:3)

您没有将webpack-cli安装到您的devDependencies

npm install webpack-cli -D

其实应该解决你的问题

也许您在全球范围内安装了webpack-cli,但它不在您的devDependencies中

答案 2 :(得分:3)

如果您直接调用函数

  "scripts": {
    "build": "webpack"
  },

从$ PATH中搜索Webpack作为命令。 使用npm install webpack-cli -D不是全局安装在$ PATH下,而是在./node_modules下本地安装。

因此,您有两个选择:

  • 全局安装(-g):npm -g i webpack-cli
  • 从node_module目录运行它:node_modules/<path to webpack>

答案 3 :(得分:1)

如果您将webpack安装到项目中而不是全局,请尝试以下方法:

"build": "./node_modules/webpack/bin/webpack"