Error: Cannot find module 'webpack'

I'm just getting started with webpack and am having difficulty getting the multiple-entry-points sample to build. The webpack.config.js file in the example includes the line

 var CommonsChunkPlugin = require("../../lib/optimize/CommonsChunkPlugin");

which fails for me with the error

Error: Cannot find module '../../lib/optimize/CommonsChunkPlugin'

Searching around, I found other examples of using the CommonsChunkPlugin with the expression

var commonsPlugin = new webpack.optimize.CommonsChunkPlugin("common.js");

which fails with the error

ReferenceError: webpack is not defined

Some more searching found a number of examples including

var webpack = require('webpack');

and my build now fails with

Error: Cannot find module 'webpack'

I'm at a loss as to how to proceed.


Link globally installed package to your project:

npm link webpack

Checkout the official documentation of yarn link.


While working on windows, I've installed webpack locally and it fixed my problem

So, on your command prompt, go to the directory of which you want to run webpack, install webpack locally (without the -g) and enjoy...


I solved the same problem by reinstalling, execute these commands

rm -Rf node_modules
rm -f package-lock.json
npm install

rmis always a dangerous command, especially with -f, please notice that before executing it!!!!!