Running webpack within project

2019-09-14 10:45发布

Do I have to install webpack globally for me to issue webpack commands?

I installed webpack inside my project folder using npm install webpack --save-dev and added it to dev dependencies. I'm inside my project folder and when I run a webpack command, I get the following error:

'webpack' is not recognized as an internal or external command, operable program or batch file.

BTW, I'm on Windows 10.

标签: webpack
3条回答
2楼-- · 2019-09-14 11:43

Yes it is obvious to get the error as you run some like:

webpack --config webpack.config.filename --progress

this is similar to running a npm install command we need npm to be installed globally for the system to know what npm is so to run webpack in similar way you need to install webpack globally. Hope this helps

查看更多
家丑人穷心不美
3楼-- · 2019-09-14 11:45

Add the command as a npm script. In your package.json.

{
    "name": "app",
    "version": "0.0.1",
    "scripts": {
      "compile": "webpack --config webpack.config.js"
    }
}

Then run

npm run compile

npm scripts looks for executable created in ./node_modules/.bin folder. When the webpack is locally installed it creates a binary in the same folder.

查看更多
欢心
4楼-- · 2019-09-14 11:47

Adding this command as a npm script like Nikhil Ranjan his answer is a good solution. Especially if you will be using this command several times.

Another way would be to directly call the executable inside the node_modules folder.

./node_modules/.bin/webpack --config webpack.config.js"
查看更多
登录 后发表回答