241

Getting error when running webpack-dev-server --config config/webpack.dev.js --progress --profile --watch --content-base src/. Here is the error log:

module.js:442 throw err; ^ Error: Cannot find module 'webpack/bin/config-yargs' at Function.Module._resolveFilename (module.js:440:15) at Function.Module._load (module.js:388:25) at Module.require (module.js:468:17) at require (internal/module.js:20:19) at Module._compile (module.js:541:32) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:458:32) at tryModuleLoad (module.js:417:12) at Function.Module._load (module.js:409:3) 
1
  • 1
    one is global version, one is local version. when webpack-dev-server, local version is used as well. npm install --save [email protected] did the job. Commented Mar 17, 2018 at 5:21

31 Answers 31

586

If you're using webpack-cli 4 or webpack 5, change webpack-dev-server to webpack serve.

Example:

"serve": "webpack serve --config config/webpack.dev.js --progress" 

You might want also check this comment on GitHub:

NPM package.json scripts are a convenient and useful means to run locally installed binaries without having to be concerned about their full paths. Simply define a script as such:

For webpack-cli 3.x:

"scripts": { "start:dev": "webpack-dev-server" } 

For webpack-cli 4.x:

"scripts": { "start:dev": "webpack serve" } 
Sign up to request clarification or add additional context in comments.

14 Comments

Do you mean changing webpack-dev-server to webpack serve?
Yes, if you are on webpack 4
this also works when combining webpack 5, webpack-cli 4 and webpack-dev-server 3 (the version treadmill never ends)
[webpack-cli] Error: Unknown option '--watch'
Don't use --watch with serve it doesn't make any sense. github.com/webpack/webpack-cli/issues/…
|
92

Jan 2021

Using webpack 5, just replace the webpack-dev-server command with webpack serve

5 Comments

And also don't remove webpack-dev-server, webpack serve needs it.
Wait, it does work, but from their official migration tutorial, we get this: serve is removed in favor of DevServer. Is this really the recommended approach? Thanks.
@cbdeveloper apparently yes, according to webpack-dev-server latest docs as well.
I don't use webpack-dev-server as command, I use it in webpack.config. I have entry 'webpack-dev-server/client?http://localhost:${port}/' but I have a same error
Tangential but if you are using Rails and @rails/webpacker, and you are OK with webpack 4.x for now, try rails webpacker:install. details
52

I've had a similar problem. I think it's related to webpack version. After changing webpack version the latest everything was fine...

8 Comments

Also worth checking webpack --version from cli if you've got it installed globally.
2020 Update: downgrading to previous versions is not a very good solution, please checkout @serhii-matrunchyk answer below
This solution won't work for anyone who wants to use webpack-dev-server because it's not compatible with webpack 5. So what you need to do instead is uninstall existing webpack-cli version and then install webpack-cli version 3.3.8 with "npm install [email protected] -D". My webpack version is 4.44.2 and dev-server version is 3.11.0 if anyone's wondering.
@cwharris Apparently your comment has deemed the title of "unsung hero" as being the 6th most copied comment across all of StackOverflow while only having a score of 5 (scroll to the end): stackoverflow.blog/2021/04/19/…
|
31

Update: March 21

Try to update your webpack dependencies with the following command

npm install --save-dev webpack webpack-cli webpack-dev-server 

if it does not work then use as following

I am having these dependencies but I faced the same issue

"webpack": "^5.6.0", "webpack-cli": "^4.2.0", "webpack-dev-server": "^3.11.0" 

And I found a solution that adding a new script or in your Start Script in package.json worked for me. So you can try this way as well.

"dev": "webpack serve --mode development --env development"

2 Comments

Your solution worked. Did you find out why webpack-dev-server is failing?
Could you just run yarn upgrade
19

This is because of the changes in webpack-cli version.

  1. If the webpack-cli version is less than 4.x, You can use webpack-dev-server
  2. If the webpack-cli version is 4.x or higher, you can use webpack serve

For webpack-cli 3.x and below

 "scripts": { "dev-server": "webpack-dev-server" } 

For webpack-cli 4.x and above

 "scripts": { "dev-server": "webpack serve" } 
 "scripts": { "dev-server": "webpack serve " } 

Source: webpack dev-server

1 Comment

Unless you're using typescript for the config files, then it's easier to just use webpack serve for all 4.x and above.
14

Solution

package.json

"scripts": { "startdev": "webpack serve --mode development --env development --hot --port 3000" ... ... }, "devDependencies": { ... "webpack": "^5.10.1", "webpack-cli": "^4.2.0" }, 

Console

$ npm run startdev 

Comments

11

Try changing the webpack version from 1.x to 2.x in your package.json:

Eg:

 "devDependencies": { "webpack": "2.2.0-rc.3", "webpack-dev-server": "2.1.0-beta.0", "webpack-validator": "^2.3.0" } 

This happens sometimes when you use pre-release version of webpack-dev-server with released version of webpack or viceversa.

1 Comment

It's Dec 2020, and I'm using ` "webpack": "^4.44.2", "webpack-cli": "^3.3.8", "webpack-dev-server": "^3.11.0"`
11

The issue is with newer webpack-cli version. If webpack-cli <= 3.x webpack-dev-server package work fine. For webpack-cli >= 4.x, use npx webpack serve command to run local server.

For webpack-cli 3.x: "scripts": { "start:dev": "webpack-dev-server --mode=development" } For webpack-cli 4.x: "scripts": { "start:dev": "webpack serve --mode=development" } 

Comments

8

I forgot to install webpack-cli. So I ran below command and issue got fixed.

npm i -D webpack-cli 

Comments

8

Update your Webpack version (and webpack CLI):

npm install --save-dev webpack webpack-cli webpack-dev-server webpack-merge 

If you don't use one of those mentioned above, feel free to omit.

1 Comment

This did it for me, thank you. Instead I ran yarn add (your command here) etc.
7

I also go this error when I had only installed webpack locally and hadn't installed it globally yet.

I had webpack-dev-server installed globally though and it had a dependency on a global install of webpack. To be fair npm did complain while installing webpack-dev-server:

UNMET PEER DEPENDENCY webpack@^2.2.0

Comments

7

In my case the solution was just use previous versions -

"webpack": "^4.44.1", "webpack-cli": "^3.3.12", "webpack-dev-server": "^3.11.0" 

Comments

7

I solved this by creating a script command on package.json.

"dev": "webpack serve --config webpack.config.js --open", 

Comments

5

I've had a similar problem. I think it's related to webpack version.

UPDATE JULY 2021

People who have versions of "webpack-cli": "^4 or above", & "webpack": "^5 or above",.

You can give a try updating your webpack version by this command

npm install --save-dev webpack webpack-cli webpack-dev-server 

Now go to you package.json, under scrpits add this line

"dev": "webpack serve --mode development --env development" 

This totally worked for me.

Comments

4

The general situation is due to Webpack and webpack-dev-server version is not compatible. Like I also have this problem, my computer's webpack is 1.15.0, but webpack-dev-server is 2.x above version. So I uninstall webpack-dev-server: npm uninstall webpack-dev-server -g Then install the 1.15.0 version of webpack-dev-server, you can solve this problem by npm install [email protected] -g

Comments

4

Try changing webpack version to 3.0 and web-dev-server to 2.7.1

Eg:

"devDependencies": { "webpack": "^3.0.0", "webpack-cli": "2.0.13", "webpack-config-utils": "2.0.0", "webpack-dev-server": "^2.7.1", "webpack-validator": "2.2.7" } 

Comments

4

I had the same problem with webpack 4.

It is version compatible issue.

To fix the issue run following command to install webpack-cli in web pack 4 .

 yarn add webpack-cli -D 

Comments

3

Deprecate the webpack-cli version by using the command :

npm install -D webpack-cli@3 

The new version is in the Beta phase and likely to fix this bug.

Comments

2

I fixed this solution by running npm start which was just a wrapper running 'webpack-dev-server' rather than running webpack-dev-server directly into the console. The problem was that I was passing options into a method I should not have been passing options into.

Running webpack-dev-server with npm start showed me the correct error message. Running webpack-dev-server directly only gave me "Error: Cannot find module 'webpack/bin/config-yargs' ". Weird.

I am on: "webpack": "^2.6.1", "webpack-dev-server": "^2.7.1"

Comments

2

I had got the following dependencies installed (without specifying any particular version)

 "webpack-cli": "^4.5.0", "webpack-dev-server": "^3.11.2" 

This error appears during the yarn start with the following entry in package.json specific to the scripts.start attr.

 "scripts": { "start": "webpack-dev-server --open", "build": "webpack" } 

So, it turns out that webpack-dev-server --open is looking for webpack-cli version 3.3. I got rid of this error by installing the specific version of webpack-cli The working package.json looks like this:

 "webpack-cli": "3.3", "webpack-dev-server": "^3.11.2" 

But, if you don't want to downgrade the webpack-cli version, update the "start": "webpack-dev-server --open" to "start": "webpack serve --open"

1 Comment

Can't believe this fixed it. "webpack": "^5.42.0", "webpack-cli": "3.3", "webpack-dev-server": "^3.11.2"
1

This is usually due to version mismatches between libraries (including webpack/yargs, in your case). This can happen a lot when you have left a project sitting for a while and some dependencies in your node_modules directory have become stale. A very simple solution, before fussing with different versions of everything, is to just move your node_modules directory to a temporary location and re-run npm install:

% mv node_modules nod_modules.REMOVED % npm install 

Then, try rerunning webpack.

Comments

1

To upgrade all packages (afer installing webpack-cli and webpack-dev-server), you can

npm --depth=9999 upgrade 

That should fix the nonmatching version problem.

Comments

1

Please use webpack serve for run webpack-dev-server

webpack serve --config config/webpack.dev.js --progress --profile --watch --content-base src/ 

Comments

1

It's webpack versioning problem You need to update your webpack using this command

npm install --save-dev webpack webpack-cli webpack-dev-server 

Now in package.json file use

"dev": "webpack serve --config webpack.config.js --open" 

1 Comment

webpack serve works, but my npm command requires a variable for some automatic things to be handled: npm run start:front -- --page home the --page home would determine which page to render. but when I do this with serve, it thinks it is an option
0

I tried the following lines and it was solved:

  1. As the issue is with webpack-dev-server so goto node-modules.
  2. find webpack-dev-server then goto dependencies
  3. check the dependency info of webpack and webpack-cli and their versions.
  4. Reinstall those names with the exact same versions.

Then try re-running the dev-server.

In my case: "dev-server": "webpack-dev-server --open"

console: npm run dev-server

Comments

0

-> So, first do you exclude the node_modules folder.
->after verificad if in the archive package.json the dependencies : "webpack", "webpack-cli" and "webpack-dev-server" they are in "dependencies":{}.
-> The end, Open the terminal execute the command : yarn. the install all depends again that was excluded.

Comments

0

use webpack serve instead of webpack-dev-server in your package.json under scripts. it perfectly work for me. I had the same error and this fixed it.

my devDependencies:

"webpack": "^5.22.0", "webpack-cli": "^4.5.0", "webpack-dev-server": "^3.11.2" 

original post : https://stackoverflow.com/a/64304022/11739552

Comments

0

I just solved it.

The problem was on path on index.html.

from:

<script src="./dist/myBundle.js"></script> 

to:

<script src="myBundle.js"></script> 

Comments

0

Changing the command from "serve": "webpack-dev-server" to "serve":"webpack serve" solved this issue. I've tried this solution with webpack dev server v3.11.0 and v3.11.2; both worked fine for me.

Comments

-1

None of the above answers worked for me. If you are still getting this error, you can try this, this fixed my problem:

Open node_modules\webpack-dev-server\bin\webpack-dev-server.js

Change Line 84: require('webpack-cli/bin/config-yargs')(yargs);

To:

require('webpack-cli/bin/config/config-yargs')(yargs);

Change Line 92: const config = require('webpack-cli/bin/convert-argv')(yargs, argv, {

To:

const config = require('webpack-cli/bin/utils/convert-argv')(yargs, argv, {

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.