56

I'm trying to use webpack-dev-server to run a simple program but I'm getting this error:

module.js:471
    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 (internal/module.js:20:19)
    at Object.<anonymous> 
    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)

I have installed webpack with following npm command

npm install --save-dev webpack

and I have the following configuration:

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

var BUILD_DIR = path.resolve(__dirname, 'client/public');
var APP_DIR = path.resolve(__dirname, 'client/app');

var config = {
  entery: APP_DIR + '/index.js',
  output: {
    path: BUILD_DIR,
    filename: 'bundle,js',
  },
  module: {
    loaders: [
      {
        test: /.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        query: {
          presets: ['es2015', 'react']
        }
      }
    ]
  }
};

module.exports = config;

I have tried everything and I'm really lost. Does anyone have any idea?

3
  • how do you run your code? Please share cmd line
    – borovsky
    Feb 15, 2017 at 17:22
  • 1
    entery has a typo it should be entry (if that was copied directly from the webpack config) Feb 16, 2017 at 3:55
  • If someone is looking for an up to date Webpack 5 tutorial, check out this: robinwieruch.de/webpack-setup-tutorial Nov 18, 2020 at 8:03

3 Answers 3

92

npm install --save-dev webpack is not enough.

You also have to install the following:

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

And optionally, you can also install:

npm install --save-dev webpack-dev-middleware webpack-hot-middleware
3
  • 5
    i have reinstalled the webpack and the webpack-dev-server and it worked thanks!
    – yonBav
    Feb 18, 2017 at 14:25
  • You are Welcome 👋.. Try also to use npm run build instead of npm build and npm run xxx instead of npm xxx .. so on. Feb 18, 2017 at 14:26
  • When running the first line, I get the error : operation not permitted, mkdir 'C:\Windows\System32\WindowsPowerShell\v1.0\node_modules
    – Markus
    Sep 10, 2019 at 8:22
14

I had the same problem, but I resolved by installing additional webpack-cli

npm install --save-dev webpack-cli
3

Try the following:

Firstly, you need to install webpack-dev-server

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

Then in your package.json, use this in your start script:

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

Note webpack.config.js should be replaced with the file name of your webpack config file.

Now run npm start

Hope it works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.