37

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
Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Taylor Austin
  • 5,407
  • 15
  • 58
  • 103
  • Possible duplicate of [Issues with getting started with webpack 4](https://stackoverflow.com/questions/48981833/issues-with-getting-started-with-webpack-4) – Borian Feb 27 '18 at 07:16
  • More related [tag:webpack-4] documentation [here](https://stackoverflow.com/a/49178441/7248949) – Carloluis Mar 10 '18 at 02:28

7 Answers7

45

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 ). 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.

Community
  • 1
  • 1
Carloluis
  • 4,205
  • 1
  • 20
  • 25
  • 1
    Also check on GitHub this [Webpack 4 Demo](https://github.com/carloluis/webpack-demo) project. Hope it helps! – Carloluis Mar 10 '18 at 04:41
8

Try This command Using Npm :

npm i -g webpack-cli -D --save
Urvashi Bhatt
  • 489
  • 5
  • 21
  • This fixed it but now im getting a different error. I will create a new post for that error. – Taylor Austin Feb 27 '18 at 16:13
  • 2
    for all curious minds, the above command install the "webpack-cli" globally instead of in your local path. i --> install; g --> global; D -->--save-dev – Kings Feb 27 '18 at 17:46
  • 1
    this could be a solution, but it is short and without explanations, the answer given by carloluis is by far more complete, and should be the answer for the question – daniel Mar 09 '18 at 16:59
2

In webpack version ^4.0.0 the webpack CLI was moved into a different package. Although this change has not been reflected in the docs, there is a pull request addressing that.

Carloluis's answer solve your problem, but I'd like to add that its recommended not to do global installs. So a simple npm install -D webpack-cli in your project folder will do.

ceoehis
  • 700
  • 8
  • 7
1

webpack team is moving things in to webpack-cli. I tried installing webpack-cli globally it gave me GIT error as shown below.

Please check if you have git installed and in your PATH.

So i first installed GIT from https://gitforwindows.org/ and then installed webpack-cli using npm.

This blog has detail screenshot and remedy to resolve this error Angular webpack GIT and webpack-cli error.

Shivprasad Koirala
  • 27,644
  • 7
  • 84
  • 73
0

I got the error saying I needed to have installed webpack-cli even when I had that already but not webpack. So I did yarn add -D webpack, no need to install it globally.

Kat
  • 1,604
  • 17
  • 24
0

I got this error when working on a Rails 6 application

The CLI moved into a separate package: webpack-cli
Please install 'webpack-cli' in addition to webpack itself to use the CLI

Here's how I solved it:

If you're using npm, run:

npm i -D webpack-cli

If you're using yarn, run:

yarn add -D webpack-cli

Note: You do not need to add/define webpack-cli in your package.json file, since the command to install it also adds webpack-cli to your devDependencies in your package.json file.

That's all

I hope this helps

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
-1

I was facing the same error. then I figured out by cloning the #webpack from git repo (https://github.com/webpack/webpack ) after that #installed the webpack-cli( npm install -g webpack webpack-cli --save-dev) using #gitbash.

last check version (webpack -v) if you get the version it is installed successfully.

Hope this will help someone like me. Thank you