Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Cannot find module 'webpack/lib/node/NodeTemplatePlugin'

Got this Error after running webpack. Webpack is installed globally and I'm running Node

PS D:\Projects\ng2-admin-master> ng serve
Cannot find module 'webpack/lib/node/NodeTemplatePlugin'
Error: Cannot find module 'webpack/lib/node/NodeTemplatePlugin'
    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> (D:\Projects\ng2-admin-master\node_modules\html-webpack-plugin\lib\compiler.js:11:26)
    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> (D:\Projects\ng2-admin-master\node_modules\html-webpack-plugin\index.js:7:21)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
PS D:\Projects\ng2-admin-master>
like image 766
Chanaka Amarasinghe Avatar asked Apr 03 '17 08:04

Chanaka Amarasinghe


3 Answers

Update (Apr 2018)

Webpack 4 onwards you are required to install webpack-cli. You may also want to install webpack-dev-middleware if you need to use the options marked with a key on this page.

In this case the command to install is:

npm install --save-dev webpack webpack-cli html-webpack-plugin webpack-dev-server webpack-dev-middleware

As mentioned above, webpack-dev-middleware should be optionally added based on your requirements.

Older answer

Node requires you to install webpack to your project.

You have 2 options to solve the above:

  1. Remove global webpack and install it locally

    npm uninstall -g webpack
    npm install --save-dev html-webpack-plugin webpack webpack-dev-server
    
  2. You can link the global webpack pkg to your project's node modules. The downside of this is that your project will be forced to use most updated webpack. This will create a problem only when some updates are not backwards compatible.

    npm i webpack -g; npm link webpack --save-dev
    

You can omit the html-webpack-plugin depending on your requirement.

You can find more info on this github issue page.

like image 156
Raunaqss Avatar answered Nov 12 '22 13:11

Raunaqss


I faced a similar issue when updating my Angular app using ncu. Finally solved it by:

  • remove the node_modules directory
  • remove package-lock.json
  • reinstall the packages.

You can run this in PowerShell (make sure you are in the correct working directory):

rm node_modules -r -force
rm package-lock.json
npm cache verify
npm install
like image 34
Martin Brandl Avatar answered Nov 12 '22 13:11

Martin Brandl


Context:

I had this problem with a React application.

I tried to uninstall webpack globally and locally, also deleted the local folder node_modules, reinstalled all local npm modules (with npm install), etc.

Nothing worked, until doing this...


Solution:

  1. Remove package-lock.json & node_modules.
  2. Don't remove the other files (like: package.json, index.js...)
  3. Install all package (npm extracts information for the installation from package.json) with npm install === npm i.
  4. Now, run your code and voila! 🎉
like image 10
Guillem Puche Avatar answered Nov 12 '22 12:11

Guillem Puche