16

I moved my project to another computer, ran npm install. Project works fine except webpack-dev-server, it throws error

module.js:544
throw err;
^

Error: Cannot find module '../lib/polyfills'
    at Function.Module._resolveFilename (module.js:542:15)
    at Function.Module._load (module.js:472:25)
    at Module.require (module.js:585:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/project/node_modules/.bin/webpack-dev-server:6:1)
    at Module._compile (module.js:641:30)
    at Object.Module._extensions..js (module.js:652:10)
    at Module.load (module.js:560:32)
    at tryModuleLoad (module.js:503:12)
    at Function.Module._load (module.js:495:3)

What is it missing ?

Alexey K
  • 6,537
  • 18
  • 60
  • 118

3 Answers3

44

The fix: rm -rf node_modules && npm install

Because..... who knows why /shrug

Kabir Sarin
  • 18,092
  • 10
  • 50
  • 41
  • using `rm -rf node_modules && npm install` gave me errors, so I deleted manually node_modules and `sudo npm install` – Karlo A. López Jun 14 '18 at 06:38
  • 1
    Yep. This is what fixed it for me. – Dr. Chocolate Oct 27 '18 at 18:27
  • 2nd time I checked this error on Stackoverflow :) ... and this answer solved it :) – maersu Oct 14 '19 at 08:30
  • 1
    for me this happened when I was `scp -r ./folder me@another-comuputer:folder` (copy project folder to another computer) so yes YARN was acting as resolved but actually dependencies were unmet (or something) ...so yes only solution is restart the node_modules by `rm -rf node_modules` and reinstall again `npm install` ... So great advise this answer. Give @sarink a cookie ! – equivalent8 Dec 11 '19 at 19:00
19

I had this exact issue and fixed it doing the following.

Running webpack-dev-server gave the error you pasted.

I thought it may have been a configuration issue, so I tried node_modules/.bin/webpack-dev-server to fix the configuration via the CLI.

Doing this, I was given the same error.

The file inside of .bin existed but something was clearly wrong. I suspect it's because I seeded this project by copying from another one and perhaps references normally set as part of npm install weren't set on the copy, so they were still pointing to the source of my copy.

Regardless, run npm install --save-dev webpack-dev-server again and try. Fixed my issue both in running webpack-dev-server and the CLI even though they were already installed. Perhaps just installed improperly.

oooyaya
  • 1,773
  • 1
  • 15
  • 39
  • 9
    Also, if you get `cannot find ../package.json` after doing this -- give up. Delete node modules and `npm install` all over again. Lesson learned: don't copy `node_modules` from project to project. – oooyaya Nov 15 '17 at 15:52
  • 1
    removing /webpack-dev-server in /node_modules/ and installing to again helped – Alexey K Nov 15 '17 at 16:00
0

I bumped my webpack-dev-server version in package.json up one dot release and ran yarn and that fixed it.

For example in package.json:

   },
   "devDependencies": {
-    "webpack-dev-server": "2.11.2"
+    "webpack-dev-server": "2.11.3"
   }
 }

Then ran yarn and the problem was fixed.

Minimul
  • 4,060
  • 2
  • 21
  • 18