Running “webpack” instead of “node_modules/.bin/we

2019-07-19 15:55发布

问题:

I'm using to run "node_modules/.bin/webpack", but I know it's possible to configure the path so that you only have to type "webpack". I can't find how, though. :/

回答1:

That would happen if you install a package globally. For webpack that would be with the command npm install -g webpack .npm in that case would install Webpack in a set location you can find with npm root -g.

If that location is in your $PATH, you can use webpack directly on your command line. Do not do that! You would proabably need different versions of webpack for different projects. Instead, if you are using NPM, use npx webpack in directory where your project / package.json is. npx webpack is a shortcut to ./node_modules/.bin/webpack. npx is already included with npm. Read more here.

Or another option is to put it in your package.json scripts property like:

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

Then you can run the local webpack with the command npm run build.NPM will then also prefer the local version over a global version if existent.

For more information, read this article: http://ericlathrop.com/2017/05/the-problem-with-npm-install-global/