node.js 错误:找不到模块“webpack/lib/node/NodeTemplatePlugin”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43179531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:08:24  来源:igfitidea点击:

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

node.jsangular

提问by GCJAmarasinghe

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

运行 webpack 后出现此错误。Webpack 是全局安装的,我正在运行 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>

回答by Raunaqss

Node requires you to install webpack to your project.

Node 要求您将 webpack 安装到您的项目中。

You have 2 options to solve the above:

您有 2 个选项可以解决上述问题:

  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

  1. 删除全局 webpack 并在本地安装

    npm uninstall -g webpack npm install --save-dev html-webpack-plugin webpack webpack-dev-server

  2. 您可以将全局 webpack pkg 链接到您项目的节点模块。这样做的缺点是你的项目将被迫使用最新的 webpack。仅当某些更新不向后兼容时,这才会产生问题。

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

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

您可以根据需要省略 html-webpack-plugin。

You can find more info on this github issue page.

您可以在此github 问题页面上找到更多信息。



Update (Apr 2018)

更新(2018 年 4 月)

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

从 Webpack 4 开始,您需要安装webpack-cli. webpack-dev-middleware如果您需要使用此页面上标有键的选项,您可能还需要安装。

In this case the command to install is:

在这种情况下,安装命令是:

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

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

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

如上所述,webpack-dev-middleware应根据您的要求选择性添加。

回答by Martin Brandl

I faced a similar issue when updateing my Angularapp using ncu. Finally solved it by removing the node_modules and the package-lock.json and reinstall the packages.

updateing我,当我遇到类似的问题角度使用的应用程序ncu。最后通过删除 node_modules 和 package-lock.json 并重新安装包来解决它。

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

您可以在 PowerShell 中运行它(确保您位于正确的工作目录中):

rm node_modules -r -force
rm package-lock.json
npm cache verify
npm install

回答by Shay

I tried for hours almost every thing suggested on different threads on Stack overflow but nothing worked. Eventually (with a lot of luck) I tried this and it worked:

我尝试了几个小时,几乎在堆栈溢出的不同线程上建议的所有内容都没有奏效。最终(运气很好)我尝试了这个并且它起作用了:

deleted node_modules library (not sure if required)
npm install -g @angular/cli
npm install @angular/cli
npm install

回答by Guillem

Context:

语境:

I've had this problem with a React application.

我在 React 应用程序中遇到了这个问题。

I've tried to uninstall webpack globally and locally, also delete the local folder node_modules, reinstall all local npm modules (with npm install), etc.

我尝试在全局和本地卸载 webpack,同时删除本地文件夹node_modules,重新安装所有本地 npm 模块(带有npm install)等。

Nothing has worked, until doing this...

没有任何效果,直到这样做......



Solution:

解决方案:

  1. Remove package-lock.json& node_modules.
  2. Not remove the other files (like: package.json, index.js...)
  3. Install all package (npmextracts information for the installation from package.json) with npm install=== npm i.
  4. Now, run your code and voila!
  1. 删除package-lock.json& node_modules
  2. 不删除其他文件(如:package.jsonindex.js...)
  3. 使用===安装所有包(从 中npm提取安装信息package.json)。npm installnpm i
  4. 现在,运行你的代码,

回答by NVCoder

Below worked for me:-

以下对我有用:-

  • Removed node_modules
  • Deleted package-lock.json
  • Run npm install
  • 删除了 node_modules
  • 删除了 package-lock.json
  • 运行 npm 安装

回答by imdzeeshan

Check webpack.config.jsand make sure all dependencies are installed.

检查webpack.config.js并确保安装了所有依赖项。

I was having the same problem too. It was resolved by installing html-webpack-plugindependency. It was defined in my webpack.config.jsbut not installed.

我也遇到了同样的问题。它是通过安装html-webpack-plugin依赖解决的。它在我的webpack.config.js但没有安装。

回答by user3198259

Remove the package-lock.json and try npm install it will resolve the issue.

删除 package-lock.json 并尝试 npm install 它将解决问题。