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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29492240/
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:38:00  来源:igfitidea点击:

Error: Cannot find module 'webpack'

node.jswebpack

提问by Ed.

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

我刚刚开始使用 webpack,并且很难构建多入口点示例。示例中的 webpack.config.js 文件包含以下行

 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

四处搜索,我发现了使用 CommonsChunkPlugin 和表达式的其他示例

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.

我不知道如何继续。

回答by Ruslanas Bal?iūnas

Link globally installed package to your project

将全局安装的包链接到您的项目

npm link webpack

回答by Ran

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

在 Windows 上工作时,我在本地安装了 webpack,它解决了我的问题

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

因此,在您的命令提示符下,转到您要运行 webpack 的目录,在本地安装 webpack(不带 -g)并享受...

回答by Jamie Treworgy

Seems to be a common Windows problem. This fixed it for me:

似乎是一个常见的 Windows 问题。这为我修复了它:

Nodejs cannot find installed module on Windows?

Nodejs 在 Windows 上找不到已安装的模块?

"Add an environment variable called NODE_PATHand set it to %USERPROFILE%\Application Data\npm\node_modules(Windows XP), %AppData%\npm\node_modules(Windows 7), or wherever npm ends up installing the modules on your Windows flavor. To be done with it once and for all, add this as a System variable in the Advanced tab of the System Properties dialog (run control.exe sysdm.cpl,System,3)."

“添加一个名为的环境变量NODE_PATH并将其设置为%USERPROFILE%\Application Data\npm\node_modules(Windows XP)、%AppData%\npm\node_modules(Windows 7) 或 npm 最终在您的 Windows 风格上安装模块的任何地方。要一劳永逸地完成它,请将其作为系统变量添加到系统属性对话框的高级选项卡(运行 control.exe sysdm.cpl,System,3)。”

Note that you can't actually use another environment variable within the value of NODE_PATH. That is, don't just copy and paste that string above, but set it to an actual resolved path like C:\Users\MYNAME\AppData\Roaming\npm\node_modules

请注意,您实际上不能在 的值中使用另一个环境变量NODE_PATH。也就是说,不要只是复制并粘贴上面的字符串,而是将其设置为实际解析的路径,例如C:\Users\MYNAME\AppData\Roaming\npm\node_modules

回答by troy

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

rm总是一个危险的命令,尤其是使用-f,请在执行之前注意!!!!!

回答by Sreen Sunki

Run below commands in Terminal:

在终端中运行以下命令:

npm install --save-dev webpack

npm install --save-dev webpack

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

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

回答by Cody Sand

I was having this issue on OS X and it seemed to be caused by a version mismatch between my globally installed webpackand my locally installed webpack-dev-server. Updating both to the latest version got rid of the issue.

我在 OS X 上遇到了这个问题,它似乎是由我的全局安装webpack和本地安装的webpack-dev-server. 将两者都更新到最新版本摆脱了这个问题。

回答by Jed Lynch

If you have installed a node package and are still getting message that the package is undefined, you might have an issue with the PATH linking to the binary. Just to clarify a binary and executable essentially do the same thing, which is to execute a package or application. ei webpack... executes the node package webpack.

如果您已经安装了一个节点包,但仍然收到该包未定义的消息,则您可能会遇到链接到二进制文件的 PATH 问题。只是为了澄清二进制文件和可执行文件本质上做同样的事情,即执行包或应用程序。ei webpack... 执行节点包 webpack。

In both Windows and Linux there is a global binary folder. In Windows I believe it's something like C://Windows/System32 and in Linux it's usr/bin. When you open the terminal/command prompt, the profile of it links the PATH variable to the global bin folder so you are able to execute packages/applications from it.

在 Windows 和 Linux 中都有一个全局二进制文件夹。在 Windows 中,我相信它类似于 C://Windows/System32,而在 Linux 中它是 usr/bin。当您打开终端/命令提示符时,它的配置文件将 PATH 变量链接到全局 bin 文件夹,以便您能够从中执行包/应用程序。

My best guess is that installing webpack globally may not have successfully put the executable file in the global binary folder. Without the executable there, you will get an error message. It could be another issue, but it is safe to say the that if you are here reading this, running webpack globally is not working for you.

我最好的猜测是全局安装 webpack 可能没有成功地将可执行文件放在全局二进制文件夹中。如果没有可执行文件,您将收到一条错误消息。这可能是另一个问题,但可以肯定地说,如果您在这里阅读本文,则全局运行 webpack 对您不起作用。

My resolution to this problem is to do away with running webpack globally and link the PATH to the node_module binary folder, which is /node_modules/.bin.

我对这个问题的解决方案是取消全局运行 webpack,并将 PATH 链接到 node_module 二进制文件夹,即 /node_modules/.bin。

WINDOWS:add node_modules/.bin to your PATH. Hereis a tutorial on how to change the PATH variable in windows.

WINDOWS:将 node_modules/.bin 添加到您的 PATH。 是有关如何在 Windows 中更改 PATH 变量的教程。

LINUX:Go to your project root and execute this...

LINUX:转到您的项目根目录并执行此...

export PATH:$PWD/node_modules/.bin:$PATH 

In Linux you will have to execute this command every time you open your terminal. Thislink here shows you how to make a change to your PATH variable permanent.

在 Linux 中,每次打开终端时都必须执行此命令。 此处的链接向您展示了如何永久更改 PATH 变量。

回答by dev.bv

On windows, I have observed that this issue shows up if you do not have administrative rights (i.e., you are not a local administrator) on the machine.

在 Windows 上,我观察到如果您在机器上没有管理权限(即,您不是本地管理员),则会出现此问题。

As someone else suggested, the solution seems to be to install locally by not using the -ghint.

正如其他人所建议的,解决方案似乎是不使用-g提示在本地安装。

回答by SAIRAM

Open npm command prompt and -- cd solution folder and then just run npm link webpackin NPM cmd prommt and re build..

打开 npm 命令提示符和 -- cd 解决方案文件夹,然后在 NPM cmd prommt 中运行npm linkwebpack 并重新构建..

回答by aermin

You can try this.

你可以试试这个。

npm install --only=dev

It works for me.

这个对我有用。