Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Cannot find module 'webpack' #299

Closed
jesperlandmer opened this issue Mar 1, 2018 · 94 comments
Closed

[Bug]: Cannot find module 'webpack' #299

jesperlandmer opened this issue Mar 1, 2018 · 94 comments

Comments

@jesperlandmer
Copy link

jesperlandmer commented Mar 1, 2018

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

@jesperlandmer jesperlandmer changed the title Can't find webpack Cannot find module 'webpack' Mar 1, 2018
@ghost
Copy link

ghost commented Mar 1, 2018

I have a similar problem:

TypeError: Cannot read property 'presetToOptions' of undefined

Described by someone else on StackOverflow

@evenstensberg
Copy link
Member

@andriibyk-apiko what version are you on?

@ghost
Copy link

ghost commented Mar 1, 2018

@ev1stensberg

"webpack": "^4.0.1",
"webpack-cli": "^2.0.9"

@evenstensberg
Copy link
Member

Mind printing your prefix paths?

@evenstensberg
Copy link
Member

Could you do a console log before the error throws on webpack?

@jesperlandmer
Copy link
Author

jesperlandmer commented Mar 1, 2018

Also had the same problem with "presetToOptions" as @andriibyk-apiko, but then I reinstalled webpack and then I got this error instead. Installed Webpack v.^2.0.0 instead and now it works.

Don't know what prefix paths is

@evenstensberg
Copy link
Member

npm config get prefix

@ghost
Copy link

ghost commented Mar 2, 2018

Mind printing your prefix paths?

/usr

@ashclarke
Copy link

ashclarke commented Mar 2, 2018

Mind printing your prefix paths?

C:\Users\<user>\AppData\Roaming\npm

[email protected] (global)
[email protected] (local)

Stacktrace:
C:\Users\<user>\AppData\Roaming\npm\node_modules\webpack-cli\bin\webpack.js:237
                            throw err;
Error: Cannot find module 'webpack'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (C:\Users\<user>\AppData\Roaming\npm\node_modules\webpack-cli\node_modules\v8-compile-cache\v8-compile-cache.js:159:20)
    at Object.<anonymous> (C:\Users\<user>\AppData\Roaming\npm\node_modules\webpack-cli\bin\convert-argv.js:7:42)
    at Module._compile (C:\Users\<user>\AppData\Roaming\npm\node_modules\webpack-cli\node_modules\v8-compile-cache\v8-compile-cache.js:178:30)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at yargs.parse.err (C:\Users\<user>\AppData\Roaming\npm\node_modules\webpack-cli\bin\webpack.js:234:14)
    at Object.parse (C:\Users\<user>\AppData\Roaming\npm\node_modules\webpack-cli\node_modules\yargs\yargs.js:539:18)
    at C:\Users\<user>\AppData\Roaming\npm\node_modules\webpack-cli\bin\webpack.js:212:8
    at Object.<anonymous> (C:\Users\<user>\AppData\Roaming\npm\node_modules\webpack-cli\bin\webpack.js:504:3)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\<source-path>\node_modules\webpack\bin\webpack.js:12:2)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)

@evenstensberg
Copy link
Member

This might be hard for me to reproduce, anyone from @webpack/core-team that can?

@ashclarke
Copy link

@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.

@imjar
Copy link

imjar commented Mar 5, 2018

use
$ webpack-cli init

@evenstensberg
Copy link
Member

@ashclarke that might be cause our prefer to global is wrong ( I think ), will investigate.

@dhruvdutt
Copy link
Member

@jesperlandmer @ashclarke Can you output npm root -g?

Related PR #332.

@ashclarke
Copy link

@dhruvdutt - C:\Users\<user>\AppData\Roaming\npm\node_modules

@evenstensberg
Copy link
Member

Can you try updating?

@quantuminformation
Copy link

quantuminformation commented Mar 20, 2018

Not sure if this is related, but if you run npm i here https://github.com/quantumjs/solar-popup/tree/master/demo

Then run webpack-cli I get

                                throw err;
                                ^

Error: Cannot find module 'webpack'

Webpack was running as normal when using webpack 3

@dhruvdutt
Copy link
Member

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

@quantuminformation
Copy link

Many thanks for this!

@ematipico
Copy link
Contributor

ematipico commented Mar 20, 2018

Is this issue still valid?

@evenstensberg
Copy link
Member

Friendly ping @jesperlandmer

@jesperlandmer
Copy link
Author

Don’t know! I think the main issue has to do with Vagrant and how Vagrant handles global values. I had several problems with other dependencies as well (e.g. Firebase). Staying with docker from now on

@evenstensberg
Copy link
Member

Okay. Let's keep this open for others to come to if they have similar issues!

@okonet
Copy link
Contributor

okonet commented Mar 28, 2018

Same issue with the global install.

npm install -g webpack-cli and running webpack-cli throws:

•100% ➜ webpack-cli
/usr/local/lib/node_modules/webpack-cli/bin/webpack.js:242
				throw err;
				^

Error: Cannot find module 'webpack'
    at Function.Module._resolveFilename (module.js:543:15)
    at Function.Module._load (module.js:470:25)
    at Module.require (module.js:593:17)
    at require (/usr/local/lib/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
    at Object.<anonymous> (/usr/local/lib/node_modules/webpack-cli/bin/convert-argv.js:7:24)
    at Module._compile (/usr/local/lib/node_modules/webpack-cli/node_modules/v8-compile-cache/v8-compile-cache.js:178:30)
    at Object.Module._extensions..js (module.js:660:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:501:12)
    at Function.Module._load (module.js:493:3)
    at Module.require (module.js:593:17)
    at require (internal/module.js:11:18)
    at yargs.parse (/usr/local/lib/node_modules/webpack-cli/bin/webpack.js:239:14)
    at Object.parse (/usr/local/lib/node_modules/webpack-cli/node_modules/yargs/yargs.js:543:18)
    at /usr/local/lib/node_modules/webpack-cli/bin/webpack.js:217:8
    at Object.<anonymous> (/usr/local/lib/node_modules/webpack-cli/bin/webpack.js:512:3)
    at Module._compile (module.js:649:30)
    at Object.Module._extensions..js (module.js:660:10)
    at Module.load (module.js:561:32)
    at tryModuleLoad (module.js:501:12)
    at Function.Module._load (module.js:493:3)
    at Function.Module.runMain (module.js:690:10)
    at startup (bootstrap_node.js:194:16)
    at bootstrap_node.js:666:3

@nebrelbug
Copy link

nebrelbug commented Aug 29, 2018

@ev1stensberg any updates? Installing webpack and webpack-cli locally doesn't work for me, and neither does npx webpack or running webpack-cli, and I tried rm -rf node_modules and then reinstall. Until then, what should I do?

@nebrelbug
Copy link

I uninstalled globally webpack and webpack-cli and then reinstalled, and it seems to be working so far...

@evenstensberg
Copy link
Member

Working on fixing this but haven’t had a lot of spare time lately. Sorry for the inconviniences.. 👨🏼‍🔧

@nebrelbug
Copy link

@ev1stensberg in the meantime, what should I do to follow best practices with my projects?

@evenstensberg
Copy link
Member

Anything that runs webpack is viable. Ideal case is to run it through node modules by installing them both locally as devDeps. If that doesn’t work, then look in the issue thread 🤗

@twentytwokhz
Copy link

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

Many thanks, it worked!

@TENsaga
Copy link

TENsaga commented Oct 11, 2018

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.

@hazone
Copy link

hazone commented Oct 18, 2018

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

@yvonnendutaw
Copy link

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

Thanks! This worked for me too!

@gogromat
Copy link

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

@mosesnova
Copy link

npm i -D webpack-cli Worked for me.

@Jacknq
Copy link

Jacknq commented Dec 11, 2018

Same issue, deleting node_modules and package-lock then npm install - worked for me

@TCarmine
Copy link

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

This worked for me in the way that it then did found finally the webpack-cli module but then I got this warnings and error:

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/

ERROR in multi ./app.js bundle.js
Module not found: Error: Can't resolve 'bundle.js' in '/home/carmine/Desktop/JS/webpack_app'
@ multi ./app.js bundle.js main[1]

@TCarmine
Copy link

@dupls
Copy link

dupls commented Mar 6, 2019

You can try running webpack directly from the node_modules folder, this seems to solve a lot of the red errors in the terminal.

./node_modules/.bin/webpack

And if you don't want to keep typing that line you can add a build script in your package.json file
"scripts": { "build": "./node_modules/.bin/webpack" ...
Then in the terminal you can just type
npm run build

@willdonner
Copy link

willdonner commented Mar 14, 2019

npm link webpack
npm link webpack-cli
https://stackoverflow.com/questions/29492240/error-cannot-find-module-webpack
Worked for me.

@cabello
Copy link

cabello commented May 28, 2019

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

@evenstensberg
Copy link
Member

Have you tried updating to the latest CLI version? Is the issue still persistent?

@cabello
Copy link

cabello commented May 29, 2019 via email

@evenstensberg
Copy link
Member

Alright. Aware this is a bug, but the bug is due the fact that you have a global and a local installation or vice versa. Could you try aligning CLI installation location with webpack location?

@heldrida
Copy link

heldrida commented Jun 26, 2019

Hey, got a new machine and I don't have webpack and webpack-cli installed globally; Debugging at the moment but seems that the reports above are correct, the bug still persists; I'll update the comment with more info after I fully understand what's going on.

The following is for a project that has a dependecy

devDependecies: {
    "foobar": "git+https://gitlab+deploy-token-1:xxxxx@yyyyyy/foobar.git#vTag",
}

{ Error: Cannot find module 'webpack-cli'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at runCommand.then (/Users/punkbit/www/temp1/node_modules/webpack/bin/webpack.js:142:5)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7) code: 'MODULE_NOT_FOUND' }

these are all the globals I have installed in the machine

npm list -g --depth 0:

├── [email protected]
└── [email protected]

Decided to cd into node_modules/foobar and tried:

npx webpack -v

One CLI for webpack must be installed. These are recommended choices, delivered as separate packages:
 - webpack-cli (https://github.com/webpack/webpack-cli)
   The original webpack full-featured CLI.
We will use "npm" to install the CLI via "npm install -D".
Do you want to install 'webpack-cli' (yes/no):

During npm install outside the node_modules/foobar I do get "One CLI for webpack must be installed...", which I accept but in the end there's the error I've reported earlier or at the top Error: Cannot find module 'webpack-cli'

which webpack
webpack not found

Fixed after:

  • testing local version through npm link
  • moved webpack, wepback-cli to dependencies not devDependencies
  • found a package that failed on import, during build command where env set to production, moved to dependencies not devDependencies
  • cleared cache --force and retested to make sure

@tkdphoenix
Copy link

#299 (comment)
This worked for me. Once I ran npm link on webpack and webpack-cli it worked after that. No need to not follow best practice and install globally.

@evenstensberg evenstensberg changed the title Bug: Cannot find module 'webpack' [Bug]: Cannot find module 'webpack' Oct 31, 2019
@monu2580
Copy link

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

@monu2580
Copy link

Screenshot from 2019-11-28 11-11-19
Screenshot from 2019-11-28 11-14-26
Screenshot from 2019-11-28 11-14-48
Screenshot from 2019-11-28 11-16-56
Screenshot from 2019-11-28 11-17-07
Screenshot from 2019-11-28 11-17-24
Screenshot from 2019-11-28 11-25-51
Screenshot from 2019-11-28 11-26-08
Screenshot from 2019-11-28 11-33-52

@halcwb
Copy link

halcwb commented Aug 23, 2020

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??

@alexander-akait
Copy link
Member

Closing due to inactivity. Please test with latest version and feel free to reopen if still regressions. Thanks!

If somebody faced with the problem again please open the new issue with reproducible example, I think we fixed it (please use webpack-cli@4)

@isaldarriaga
Copy link

i was able to solve this problem in fedora 33 this way:

  1. install nvm
  2. nvm install 10.23.3 # node version!
  3. cd <your node path>
  4. rm -Rf node_modules/
  5. gedit package.json
  6. Add "webpack": "^4.39.3" to "devDependencies"
  7. npm install
  8. npm start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests