Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack: After installing webpack and webpack-cli still getting error when running webpack

I have the latest versions of webpack installed:

"webpack": "^4.0.0", "webpack-cli": "^2.0.9" 

Yet when I run webpack in the terminal I get the following:

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 
like image 774
Taylor Austin Avatar asked Feb 26 '18 15:02

Taylor Austin


People also ask

Is webpack CLI required?

If you're using webpack v4 or later and want to call webpack from the command line, you'll also need to install the CLI.

What is the use of webpack CLI?

webpack CLI provides a flexible set of commands for developers to increase speed when setting up a custom webpack project. As of webpack v4, webpack is not expecting a configuration file, but often developers want to create a more custom webpack configuration based on their use-cases and needs.

How do I uninstall webpack command line?

If using the Webpack CLI causes errors, it's probably because there's a different version installed globally. To uninstall it globally, type npm -g uninstall webpack .


2 Answers

Seems that you had installed globally only webpack and not webpack-cli.

Therefore, npm install -g webpack-cli solves the issue.


Explanation and alternative solutions:

Why there is the problem in the first place? The following indicates that both webpack and webpack-cli packages are locally installed:

I have the latest versions of webpack installed:

   "webpack": "^4.0.0",    "webpack-cli": "^2.0.9" 

Running webpack in your terminal cannot find your locally installed version (provided by webpack-cli since webpack-4). That's because your locals executables folder aren't included in your shell PATH variable (list of directories in which the shell looks for commands). The path where npm install executables locally is ./node_modules/.bin (more info here).

Therefore, instead of try running just webpack you need to run:

./node_modules/.bin/webpack 

Also, adding to your package.json a script which use just webpack works because npm adds local ./node_modules/.bin/ directory to the shell path before it executes scripts (see npm run).

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

Then, execute in your terminal: npm run build

In recap, I think the package.json script is the more clear and desirable way to go.

like image 175
Carloluis Avatar answered Sep 26 '22 23:09

Carloluis


Try This command Using Npm :

npm i -g webpack-cli -D --save 
like image 37
Urvashi Bhatt Avatar answered Sep 22 '22 23:09

Urvashi Bhatt