176

I am Learning React.js and i am using windows 8 OS.i have navigate to my root folder

1.Created the package.json file by npm init
2. install webpack by npm install -S webpack.now webpack has been downloaded to my modules folder
3. install webpack globally by typing npm install webpack -g
4. i am also having a webpack.config.js in my root folder which contains the source and ouput directory
5. when i type the webpack command i am getting the below error.

webpack is not recognized as a internal or external command,operable program or batch file

yasar
  • 2,651
  • 4
  • 18
  • 18

25 Answers25

223

Better solution to this problem is to install Webpack globally.

This always works and it worked for me. Try below command.

npm install -g webpack
DrDom
  • 4,033
  • 1
  • 21
  • 23
srikanth_k
  • 2,807
  • 3
  • 16
  • 18
  • 50
    I think it should be noted that using the -g installs webpack globally, which you may not want if you have multiple projects that may require different versions of webpack. – Uber Schnoz Mar 13 '17 at 17:59
  • 13
    I also ran `npm install -g webpack-cli`. Now it works. – Derk Jan Speelman Nov 11 '21 at 11:53
  • The [docs](https://www.npmjs.com/package/webpack-dev-server/v/4.9.3) recommend local installation over global. "`webpack-dev-server` will always use a local installation over a global one." – Vicktor Feb 04 '23 at 07:11
103

As an alternative, if you have Webpack installed locally, you can explicitly specify where Command Prompt should look to find it, like so:

node_modules\.bin\webpack

(This does assume that you're inside the directory with your package.json and that you've already run npm install webpack.)

  • 4
    Agree with Max, the reason being that it is recommended to have webpack installed locally (in devDependencies) - My problem was a little different though, when adding webpack as a pre build step in VS 2017, I thought VS was smart enough to find the webpack cmd locally without the full path – JimiSweden Dec 15 '17 at 09:23
  • @JimiSweden did you try adding `node_modules\.bin` to _tools->configure external tools_ – Max Favilli Jan 15 '18 at 19:21
  • 7
    You could try using `npx webpack` which also checks `./node_modules/.bin` instead of fiddling with paths. – Manfred Jan 18 '19 at 20:06
  • This is the only answer that has worked for me as of yet. – Matthew Wolman Sep 21 '20 at 21:52
91

I had this issue for a long time too. (webpack installed globally etc. but still not recognized) It turned out that I haven't specified enviroment variable for npm (where is file webpack.cmd sitting) So I add to my Path variable

%USERPROFILE%\AppData\Roaming\npm\

If you are using Powershell, you can type the following command to effectively add to your path :

[Environment]::SetEnvironmentVariable("Path", "$env:Path;%USERPROFILE%\AppData\Roaming\npm\", "User")

IMPORTANT : Don't forget to close and re-open your powershell window in order to apply this.

Vlado
  • 1,052
  • 10
  • 8
58

npm install -g webpack-dev-server will solve your issue

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Araali Farooq
  • 662
  • 6
  • 4
  • 2
    It may solve the error message OP posted, but this is not a good solution to the problem as it adds more dependencies than _just_ solving the missing webpack. See any of the other high-voted answers for better alternatives. – angularsen Jan 04 '19 at 11:59
  • 2
    As pointed out by different contributors already (see other answers/comments), installing globally is considered bad practice as it locks you down to one version. See also https://webpack.js.org/guides/installation/ – Manfred Jan 18 '19 at 20:07
  • I faced this problem and your solution work very nicely to solve my problem `'webpack-dev-server' is not recognized as an internal or external command, operable program or batch file. ` – Neloy Sarothi Feb 24 '21 at 07:16
34

Try deleting node_modules in local directory and re-run npm install.

Dushyant Singh
  • 395
  • 3
  • 6
  • This worked for me because I previously deleted `.bin` folder to fix an issue with `npm install` which introduced this issue instead. – Nacho H Sep 27 '22 at 22:20
26

Maybe a clean install will fix the problem. This "command" removes all previous modules and re-installs them, perhaps while the webpack module is incompletely downloaded and installed.

npm clean-install
16

Add webpack command as an npm script in your package.json.

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

Then run

npm run compile

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

Nikhil Ranjan
  • 994
  • 12
  • 16
13

Webpack CLI is now in a separate package and must be installed globally in order to use the 'webpack' command:

npm install -g webpack-cli

EDIT: Much has changed. Webpack folks do not recommend installing the CLI globally (or separately for that matter). This issue should be fixed now but the proper install command is:

npm install --save-dev webpack

This answer was originally intended as a "work-around" for the OPs problem.

Ibn Masood
  • 1,093
  • 1
  • 14
  • 31
  • 1
    "must be installed globally" is not correct as I understand it. Even the webpack folks advise against it. See https://webpack.js.org/guides/installation/ – Manfred Jan 18 '19 at 20:09
6

We also experienced this problem and I like all the answers that suggest using a script defined in package.json.

For our solutions we often use the following sequence:

  1. npm install --save-dev webpack-cli (if you're using webpack v4 or later, otherwise use npm install --save-dev webpack, see webpack installation, retrieved 19 Jan 2019)
  2. npx webpack

Step 1 is a one-off. Step 2 also checks ./node_modules/.bin. You can add the second step as a npm script to package.json as well, for example:

{
   ...
   "scripts": {
      ...
      "build": "npx webpack --mode development",
      ...
   },
   ...
}

and then use npm run build to execute this script.

Tested this solution with npm version 6.5.0, webpack version 4.28.4 and webpack-cli version 3.2.1 on Windows 10, executing all commands inside of a PowerShell window. My nodejs version is/was 10.14.2. I also tested this on Ubuntu Linux version 18.04.

I'd advise against installing webpack globally, in particular if you are working with a lot of different projects each of which may require a different version of webpack. Installing webpack globally locks you down to a particular version across all projects on the same machine.

Manfred
  • 5,320
  • 3
  • 35
  • 29
6
npx webpack

It is worked for me. I'm using Windows 10 and I installed webpack locally.

aystiro
  • 73
  • 1
  • 6
5

For me it worked to install webpack separately. So simply:

$npm install
$npm install webpack

I'm not sure why this should be necessary, but it worked.

Katinka Hesselink
  • 3,961
  • 4
  • 20
  • 26
4

This below-given commands worked for me.

npm cache clean --force
npm install -g webpack

Note - Run these commands as administrator. Once installed then close your command prompt and restart it to see the applied changes.

Nand
  • 611
  • 6
  • 12
3

Just run your command line (cmd) as an administrator.

ZOALIT
  • 55
  • 2
3

I've had same issue and just added the code block into my package.json file;

 "scripts": {
   "build": "webpack -d --progress --colors"
 }

and then run command on terminal;

npm run build
B.Brave
  • 35
  • 1
  • 3
3

you have to install webpack and webpack-cli in the same scope.

npm i -g webpack webpack-cli

or,

npm i webpack webpack-cli

if you install it locally you need to call it specifially

node_modules/.bin/webpack -v
Irteza Asad
  • 1,145
  • 12
  • 4
3

Install WebPack globally

npm install --global webpack
Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55
3

I had this issue when upgrading to React 16.12.0.

I had two errors one regarding webpack and the other regarding the store when rendering the DOM.

Webpack Error:

webpack is not recognized as a internal or external command,operable program or batch file

Webpack Solution:

  1. Close related VS Solution
  2. Delete node_modules folder
  3. Deleted package-lock.json
  4. npm install
  5. npm rebuild
  6. Repeated this 2-3 times

Store Error:

Type Store<()> is not assignable to type Store<any, AnyAction>

Store Solution:

Suggestions to update my React version didn't fix this error for me, but irrespective I would recommend doing it.

My code ended up looking like this:

ReactDOM.render(
        <Provider store={store as any}>
            <ConnectedApp />
        </Provider>,
        document.getElementById('app')
    );

As per this solution

EGC
  • 1,719
  • 1
  • 9
  • 20
3

If you create a boilerplate folder for your JS projects so that you can use JS Modules, webpack and Babel are great tools.

Don't install webpack globally and after installing the most recent versions of both, your package.json file will be loaded up and ready to copy for future projects.

Make sure to delete the node_modules folder to decrease file size in your boilerplate folder and then to reinstall node_modules use npm install.

I forgot to run npm install and kept getting this error when trying to run my webpack dev-server until I realized I needed to run npm install to install node_modules and then it worked.

Sandeep Kumar
  • 2,397
  • 5
  • 30
  • 37
2

If you have just cloned a repo, you first need to run

npm install

The error your getting will be generated if you are missing project dependencies. The above command will download and install them.

PrestonDocks
  • 4,851
  • 9
  • 47
  • 82
2

I also Face the same issue this command works for me

npm install --save-dev webpack

Umer Aziz
  • 111
  • 5
1

I got the same error, none of the solutions worked for me, I reinstalled node and that repaired my environment, everything works again.

Simon Xiao
  • 11
  • 1
  • This is what I had to do as well. I tried at least three other "solutions" provided on here and none worked except for uninstalling and reinstalling Node again. – Quiver Apr 12 '19 at 14:44
0

Try this folks, the cli needs to be updated to the latest version

npm install --save-dev @angular/cli@latest

credit goes go to R.Richards https://stackoverflow.com/a/44526528/1908827

Akash Yellappa
  • 2,126
  • 28
  • 21
0

The fix for me was locally installing webpack as devDependency. Although I have it as devDependencies it was not installed in node_modules folder. So I ran npm install --only=dev

Seagull
  • 1,063
  • 1
  • 11
  • 18
-1

Sometimes npm install -g webpack does not save properly. Better to use npm install webpack --save . It worked for me.

  • 1
    -g installs globally (not your local project node_modules + package.json) while --save installs locally (in your local node_modules + package.json) so this answer is wrong. – George Feb 09 '18 at 15:58
-1

I had this same problem and I couldn't figure it out. I went through every line of code and couldn't find my error. Then I realized that I installed webpack in the wrong folder. My error was not paying attention to the folder I was installing webpack to.

Michael Neely
  • 361
  • 4
  • 5