webpack-cli: [Bug]: Cannot find module 'webpack'

Do you want to request a feature or report a bug? Bug

What is the current behavior? Whenever I try to run webpack, I get this error message:

`vagrant@vagrant-ubuntu-trusty-32:/vagrant$ webpack
/usr/lib/node_modules/webpack-cli/bin/webpack.js:237
                                throw err;
                                ^

Error: Cannot find module 'webpack'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Module.require (module.js:587:17)
    at require (/usr/lib/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
    at Object.<anonymous> (/vagrant/webpack.config.tsx:1:77)
    at Module._compile (/usr/lib/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:178:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (/usr/lib/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
    at WEBPACK_OPTIONS (/usr/lib/node_modules/webpack-cli/bin/convert-argv.js:133:13)
    at requireConfig (/usr/lib/node_modules/webpack-cli/bin/convert-argv.js:135:6)
    at /usr/lib/node_modules/webpack-cli/bin/convert-argv.js:142:17
    at Array.forEach (<anonymous>)
    at module.exports (/usr/lib/node_modules/webpack-cli/bin/convert-argv.js:140:15)
    at yargs.parse (/usr/lib/node_modules/webpack-cli/bin/webpack.js:234:39)
    at Object.parse (/usr/lib/node_modules/webpack-cli/node_modules/yargs/yargs.js:539:18)
    at /usr/lib/node_modules/webpack-cli/bin/webpack.js:212:8
    at Object.<anonymous> (/usr/lib/node_modules/webpack-cli/bin/webpack.js:504:3)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/usr/lib/node_modules/webpack/bin/webpack.js:12:2)
    at Module._compile (module.js:643:30)

If the current behavior is a bug, please provide the steps to reproduce. I’m using Vagrant, and just installed webpack and webpack cli globally. Using:

node.js: v8.9.4 npm: v5.6.0 webpack: v4.0.1 webpack-cli: v2.0.9

What is the expected behavior? To bundle the files

Please mention other relevant information such as the browser version, Node.js version, Operating System and programming language. I’m using Typescript and React on a Node machine. Vagrant runs on Ubuntu

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 35
  • Comments: 94 (41 by maintainers)

Most upvoted comments

I have the same problem And this command works with me: npm uninstall -g webpack webpack-cli npm install -g webpack webpack-cli

@QuantumInformation You missed installing webpack-cli. Just do npm i -D webpack-cli inside your demo dir and you’re set. 💯

I only got it to work by first install npm install -g webpack-cli and then npm install -g webpack.

Install webpack-cli and yarn add npm install webpack-cli -D yarn add webpack-cli -D

probably covered already, but just in case… I’m a new webpack user and I installed webpack globally via npm and I installed webpack-cli within my local project’s package.json dependencies. That is when the error was occurring. I uninstalled the local cli and then installed the cli globally. No more error. I think they both need to be in the same environment. This thread helped me figure it out. Thanks!

Ran into this myself just now, none of the comments above have helped… Looking for clarification…

  • Is -g not recommended ?
npm uninstall -g webpack webpack-cli
npm install -g webpack webpack-cli
webpack

Error: Cannot find module 'webpack'

OP appears to be using Unix, but I had the same error after multiple project restarts/npm installs. For those using WSL / Bash for Windows I noticed sometimes npm install fails to install some of the binaries. Webpack and webpack-dev-server were missing in my case.

ls node_modules/.bin/  

// if the binaries are missing run any combo of:
npm i -D webpack webpack-cli webpack-dev-server 

// we had to modify our package.json to access the binary with the full path (instead of just npx webpack):
build:dev: "npx ./node_modules/.bin/webpack --config ./webpack.config/service.dev.js --watch",

Spent half a day trying to figure this out and ran through most of the suggestions here, hopefully it helps someone.

I have run webpack several times on my project but suddenly throw this Error I tried to install babel-preset-react but throw Error. Then, i run webpack in project it throws this Error below

module.js:540 throw err; ^

Error: Cannot find module ‘webpack’ at Function.Module._resolveFilename (module.js:538:15) at Function.Module._load (module.js:468:25) at Module.require (module.js:587:17) at require (internal/module.js:11:18) at Object.<anonymous> (C:\Program Files\nodejs\node_modules\webpack-dev-server\lib\Server.js:22:17) at Module._compile (module.js:643:30) at Object.Module._extensions…js (module.js:654:10) at Module.load (module.js:556:32) at tryModuleLoad (module.js:499:12) at Function.Module._load (module.js:491:3) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! more-recipe-server@1.0.0 start: webpack-dev-server --mode development --open npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the more-recipe-server@1.0.0 start script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\duntu\AppData\Roaming\npm-cache_logs\2018-05-18T18_41_35_681Z-debug.log

@ev1stensberg I am receiving an issue which I think might be related to this one. I found an SO about it and after some playing around managed to workaround the issue by NOT installing webpack-cli globally.

At this time I feel like webpack-cli globally only works if webpack is also installed globally? But I’m not absolutely certain about that.

I am using:

"webpack": "^4.8.1",
"webpack-cli": "^2.1.3"

Essentially trying to run the global webpack-cli against the local project results in Cannot find module 'webpack/schemas/WebpackOptions.json'

Please have a look at https://stackoverflow.com/questions/49974837/error-while-running-react-application/50281907 for more details.

use $ webpack-cli init

npm config get prefix

So, I tried about everthing in this thread:

  • uninstalled the global webpack, using only local install
  • updated to the latest versions
  • linked webpack and webpack-cli

No luck!! Only directly running with node node_modules/webpack-dev-server/bin/webpack-dev-server.js works.

Maybe a bug with > 91 comments is an indication??

i was getting the same error " [Bug]: Cannot find module ‘webpack’ " i fixed this Error after following these steps: Step-1: Run this command to install webpack

 npm install --save-dev webpack webpack-cli

Step-2: uninstall Webpack

 npm uninstall --save-dev webpack webpack-cli

step-3: Delete node-module folder from your project directory

 rm -rf node_modules

step-4: Run npm install

 npm install

step-5: Run yarn command

 yarn

step-6: Run start command

yarn  start

or

npm start

This is working for me

I likely cannot provide much insight but this was affecting me not locally on macOS but on my continuous integration provider linux/Ubuntu, after debugging with SSH in the CI, I found out that webpack-cli 3.2.0 was the first version to introduce this issue, so I downgraded it to 3.1.2 in my package.json:

- "webpack-cli": "3.2.0",
+ "webpack-cli": "3.1.2",

And it worked! I also tried many webpack 4.X and they all seemed to work with cli 3.1.2.


CI debugging data:

Docker Engine Version: 18.09.1
Kernel Version: Linux 31e94c1fa4b9 4.15.0-1035-aws #37-Ubuntu SMP Mon Mar 18 16:15:14 UTC 2019 x86_64 Linux
Starting container node:10.12.0

Note: installing webpack globally is not recommended. For now I reference it as node_modules/webpack/bin/webpack.js in package.json/scripts section

I had this happen, similar to the experience here: https://github.com/webpack/webpack-cli/issues/299#issuecomment-376839242

TLDR: Make sure webpack + webpack-cli are installed, rm -rf node_modules, and reinstall. worked in my case. (yours may very)

I use yarn. And yarn workspaces.

But the gist of it is I made sure webpack and webpack-cli were both in package.json. 4.5.0 and 2.0.14 worked for me. Locally installed (not npm i -g)

I invoke webpack via ./node_modules/.bin/webpack (normal for me, since 3.x)

And, when this error happened, I just rm -rf node_modules (in all descendant workspace projects, too. e.g. rm -rf ./workspacea/node_modules) and reinstalled again via $ yarn. This wiped away whatever cruft was there before. That’s what made it work for me.

Also if you’re troubleshooting: double check to make sure you pick one or the other: a locally installed webpack+webpack-cli, or a system one. If you go local, sure you wipe out [sudo] npm uninstall -g webpack webpack-cli.

(I still don’t know whether it’s best / wrong / right to use webpack on system’s node_modules or local.)

@ev1stensberg - Not sure it helps, but it seems to only affect the global install.

I installed webpack-cli locally and it works, but you get the “prefer global” warning.

Webpack-cli will try to default to your local installation, which makes the error appear. I’ll try to push a fix to this as soon as possible.

I was having the same issue in Docker, but only on Mac and Linux. Windows was fine.

As a temporary fix, I have had to add webpack and webpack-cli to the “dependencies” section in package.json (rather than just the devDependencies section).

I tried many different combinations in the dockerfile to install webpack during the build, but none of them worked except for this.