无法用'npm run build'替换命令'webpack'
请有人向我解释一下。我想使用命令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! [email protected] build: `webpack`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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
回答如下:
如果你直接调用一个函数
"scripts": {
"build": "webpack"
},
Webpack作为来自$ PATH的命令进行搜索。使用npm install webpack-cli -D
不是全局安装在$ PATH下,而是在./node_modules
下本地安装。
因此,您有两个选择:
- 全局安装(-g):
npm -g i webpack-cli
- 从node_module目录运行它:
node_modules/<path to webpack>