136

I am working on a React webapp using webpack, loosely alongside this tutorial.

Accidentally, I added the node_modules folder to my git. I then removed it again using git rm -f node_modules/*.

Now, when I try starting the webpack server, I get the following error:

> webpack-dev-server -d --config webpack.dev.config.js --content-base public/ --progress --colors

sh: webpack-dev-server: command not found

npm ERR! Darwin 14.4.0
npm ERR! argv "node" "/usr/local/bin/npm" "run" "devserve"
npm ERR! node v0.12.4
npm ERR! npm  v2.10.1
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! Blabber@0.0.1 devserve: `webpack-dev-server -d --config webpack.dev.config.js --content-base public/ --progress --colors`
npm ERR! spawn ENOENT

At first I thought it was only my project, but then I checked out the code checkpoints of the tutorial: same error! So something seems to be messed up globally.

Here's what I tried so far:

  • rm node_modules and reinstall with npm install
  • npm cache clean as someone mentioned regarding this issue on github
  • install webpack globally with npm install -g webpack
  • completely delete node and npm from my system (using this guide) and reinstall using brew

The error message still persists. What else can I try?

PS: The content of webpack.dev.config.js is:

var config = require('./webpack.config.js');
var webpack = require('webpack');

config.plugins.push(
  new webpack.DefinePlugin({
    "process.env": {
      "NODE_ENV": JSON.stringify("development")
    }
  })
);

module.exports = config;
Community
  • 1
  • 1
Jonas Kemper
  • 3,745
  • 3
  • 14
  • 21

12 Answers12

206

Okay, it was easy:

npm install webpack-dev-server -g

What confused me that I did not need that at first, probably things changed with a new version.

Zhongyuan Zhou
  • 339
  • 3
  • 14
Jonas Kemper
  • 3,745
  • 3
  • 14
  • 21
  • 30
    For some reason it didn't work for me without the `-g` flag. So: `npm i webpack-dev-server -g` solved the problem. – Martin Velchevski Jan 19 '16 at 16:01
  • 2
    I too required the -g flag – Alex Grande Jul 02 '16 at 18:46
  • 5
    I also needed to npm install webpack -g because it was not installed globally at first – TJ Trapp Feb 03 '17 at 06:22
  • 2
    It was confusiong because on the webpack page they don't have the -g option in their example. If you dont want to install globally, you can create a script in the package.json and run it through npm run. – kingd9 May 27 '17 at 12:46
  • i try uninstall this package using this command "npm uninstall webpack-dev-server -g --save" but not uninstalled, please let me know how to uninstall this package. – Bhavin Nov 26 '18 at 11:18
  • That solved it for me! Thanks you. – Joe Jan 05 '22 at 19:32
43

FYI, to access any script via command-line like you were trying, you need to have the script registered as a shell-script (or any kind of script like .js, .rb) in the system like these files in the the dir /usr/bin in UNIX. And, system must know where to find them. i.e. the location must be loaded in $PATH array.


In your case, the script webpack-dev-server is already installed somewhere inside ./node_modules directory, but system does not know how to access it. So, to access the command webpack-dev-server, you need to install the script in global scope as well.

$ npm install webpack-dev-server -g

Here, -g refers to global scope.


However, this is not recommended way because you might face version conflicting issues; so, instead you can set a command in npm's package.json file like:

  "scripts": {
    "start": "webpack-dev-server -d --config webpack.dev.config.js --content-base public/ --progress --colors"
   }

This setting will let you access the script you want with simple command

$ npm start

or

$ yarn start

So short to memorize and play. And, npm knows the location of the module webpack-dev-server.

Shiva
  • 11,485
  • 2
  • 67
  • 84
  • But when I run `node_modules/.bin/webpack-dev-server`,why it said that this is invalid command?I think running `node_modules/.bin/webpack-dev-server` is equal to running `npm run dev` – Chor Oct 22 '19 at 12:32
  • 1
    In the readme for webpack-dev-server - they recommend local vs global install! – JGFMK Dec 24 '20 at 13:26
17

The script webpack-dev-server is already installed inside ./node_modules directory. You can either install it again globally by

sudo npm install -g webpack-dev-server

or run it like this

./node_modules/webpack-dev-server/bin/webpack-dev-server.js -d --config webpack.dev.config.js --content-base public/ --progress --colors

. means look it in current directory.

Fangxing
  • 5,716
  • 2
  • 49
  • 53
12

Yarn

I had the problem when running: yarn start

It was fixed with running first: yarn install

pme
  • 14,156
  • 3
  • 52
  • 95
6

I had the same issue but the below steps helped me to get out of it.

  1. Installing the 'webpack-dev-server' locally (In the project directory as it was not picking from the global installation)

    npm install --save webpack-dev-server

Can verify whether 'webpack-dev-server' folder exists inside node_modules.

  1. Running using npx for running directly

npx webpack-dev-server --mode development --config ./webpack.dev.js

npm run start also works fine where your entry in package.json scripts should be like the above like without npx.

RajaSekhar K
  • 127
  • 2
  • 5
2

I found the accepted answer does not work in all scenarios. To ensure it 100% works one must ALSO clear the npm cache. Either directly Goto the cache and clear locks, caches, anonymous-cli-metrics.json; or one can try npm cache clean.

Since the author had cleared the cache before trying the recommended solution its possible that failed to make it part of the pre-requisites.

ArjunDhar
  • 39
  • 6
2

For global installation : npm install webpack-dev-server -g

For local installation npm install --save-dev webpack

When you refer webpack in package.json file, it tries to look it in location node_modules\.bin\

After local installation, file wbpack will get created in location: \node_modules\.bin\webpack

S_K
  • 749
  • 1
  • 7
  • 10
1

I noticed the same problem after installing VSCode and adding a remote Git repository. Somehow the /node_modules/.bin folder was deleted and running npm install --save webpack-dev-server in the command line re-installed the missing folder and fixed my problem.

Goz E.
  • 81
  • 1
  • 5
0

I install with npm install --save-dev webpack-dev-server then I set package.json and webpack.config.js like this: setting.

Then I run webpack-dev-server and get this error error.

If I don't use npm install -g webpack-dev-server to install, then how to fix it?

I fixed the error configuration has an unknown property 'colors' by removing colors:true. It worked!

iled
  • 2,142
  • 3
  • 31
  • 43
0

I had similar problem with Yarn, none of above worked for me, so I simply removed ./node_modules and run yarn install and problem gone.

ulou
  • 5,542
  • 5
  • 37
  • 47
0

Use this:-

npm install --save-dev babel-loader url-loader webpack webpack-cli webpack-dev-server --legacy-peer-deps
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 26 '22 at 12:36
-1

npm install webpack-dev-server -g -- windows OS

Better you use sudo in linux to avoid permission errors

sudo npm install webpack-dev-server -g

You could use sudo npm install webpack-dev-server --save to add it to package.json.

Sometimes you may require to run the below commands.

npm install webpack-cli --save or npm install webpack-cli -g

Sreenivasula Reddy
  • 149
  • 1
  • 1
  • 8