简体   繁体   English

错误:找不到模块“webpack”

[英]Error: Cannot find module 'webpack'

I'm just getting started with webpack and am having difficulty getting the multiple-entry-points sample to build.我刚刚开始使用 webpack 并且很难构建多入口点示例 The webpack.config.js file in the example includes the line示例中的 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.我不知道如何进行。

Link globally installed package to your project:将全局安装的包链接到您的项目:

npm link webpack

Checkout the official documentation of yarn link .查看yarn link官方文档

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)并享受...

I solved the same problem by reinstalling, execute these commands我通过重新安装解决了同样的问题,执行这些命令

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

rm is always a dangerous command, especially with -f , please notice that before executing it!!!!! rm始终是一个危险的命令,尤其是使用-f时,请注意在执行之前!!!!!!

在终端中运行以下命令:

npm install --save-dev webpack

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

Seems to be a common Windows problem.似乎是一个常见的 Windows 问题。 This fixed it for me:这为我修复了它:

Nodejs cannot find installed module on Windows? Nodejs 在 Windows 上找不到已安装的模块?

"Add an environment variable called NODE_PATH and 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 .请注意,您实际上不能在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也就是说,不要只是复制并粘贴上面的字符串,而是将其设置为实际解析的路径,如C:\Users\MYNAME\AppData\Roaming\npm\node_modules

I was having this issue on OS X and it seemed to be caused by a version mismatch between my globally installed webpack and my locally installed webpack-dev-server .我在 OS X 上遇到了这个问题,这似乎是由我的全局安装webpack和我的本地安装webpack-dev-server之间的版本不匹配引起的。 Updating both to the latest version got rid of the issue.将两者更新到最新版本即可解决此问题。

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.如果您已经安装了一个节点包并且仍然收到该包未定义的消息,那么您可能在 PATH 链接到二进制文件时遇到了问题。 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. ei webpack... 执行节点包webpack。

In both Windows and Linux there is a global binary folder.在 Windows 和 Linux 中都有一个全局二进制文件夹。 In Windows I believe it's something like C://Windows/System32 and in Linux it's usr/bin.在 Windows 中我相信它类似于 C://Windows/System32 而在 Linux 中它是 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.当您打开终端/命令提示符时,它的配置文件将 PATH 变量链接到全局 bin 文件夹,以便您能够从中执行包/应用程序。

My best guess is that installing webpack globally may not have successfully put the executable file in the global binary folder.我最好的猜测是全局安装 webpack 可能没有成功地将可执行文件放入全局二进制文件夹中。 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 对您不起作用。

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. WINDOWS:将 node_modules/.bin 添加到您的 PATH。 Here is a tutorial on how to change the PATH variable in windows.是关于如何在 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.在 Linux 中,每次打开终端时都必须执行此命令。 This link here shows you how to make a change to your PATH variable permanent.此处的链接向您展示如何永久更改 PATH 变量。

全局安装 webpack 和 CLI 对我有用。

npm i -g webpack webpack-cli

I was facing same problem, and I solved through this command, check this out will solve your issue.我遇到了同样的问题,我通过这个命令解决了,检查一下会解决你的问题。

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

On windows, I have observed that this issue shows up if you do not have administrative rights (ie, 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 -g hint.正如其他人所建议的那样,解决方案似乎是不使用-g提示在本地安装。

for me, it is a wrong error feedback.对我来说,这是一个错误的错误反馈。

there was config error in webpack.config.js , webpack.config.js中存在配置错误,

delete the file and start over solved my issue删除文件并重新开始解决了我的问题

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

You can try this.你可以试试这个。

npm install --only=dev

It works for me.这个对我有用。

Nothing suggested above worked for me (including the NODE_PATH variable).上面没有任何建议对我有用(包括 NODE_PATH 变量)。 I created a sym link of "node_modules" from my local folder to the global AppData(eg below) and it worked like charm.我创建了一个从本地文件夹到全局 AppData(例如下面)的“node_modules”符号链接,它就像魅力一样。

C:\Users\mmoinuddin\AppData\Roaming\npm>mklink /D node_modules c:\essportreact\day1\node_modules
symbolic link created for node_modules <<===>> c:\essportreact\day1\node_modules
C:\essportreact\day1>webpack
Hash: 2a82a67f90f9aa05ab4a
Version: webpack 1.15.0

Just found out that using Atom IDE terminal did not install dependencies locally (probably a bug or just me).刚刚发现使用Atom IDE 终端并没有在本地安装依赖项(可能是一个错误或只是我)。 Installing git bash externally and running npm commands again worked for me在外部安装 git bash 并再次运行 npm 命令对我有用

I had a ton of issues getting a very simple .NET Core 2.0 application to build in VS 2017. This is the error from AppVeyor, however it was essentially the same thing locally (some paths omitted for security) :在 VS 2017 中构建一个非常简单的 .NET Core 2.0 应用程序时,我遇到了很多问题。这是来自 AppVeyor 的错误,但它在本地基本上是一样的(为了安全,省略了一些路径):

Performing first-run Webpack build...执行首次运行的 Webpack 构建...

module.js:327 throw err; module.js:327 抛出错误;

EXEC : error : Cannot find module '......../node_modules/webpack/bin/webpack.js'执行:错误:找不到模块'......../node_modules/webpack/bin/webpack.js'

at Function.Module._resolveFilename (module.js:325:15)在 Function.Module._resolveFilename (module.js:325:15)

at Function.Module._load (module.js:276:25)在 Function.Module._load (module.js:276:25)

at Function.Module.runMain (module.js:441:10)在 Function.Module.runMain (module.js:441:10)

at startup (node.js:140:18)启动时(node.js:140:18)

at node.js:1043:3在 node.js:1043:3

csproj(25,5): error MSB3073: The command "node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js" exited with code 1. csproj(25,5):错误 MSB3073:命令“node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js”以代码 1 退出。

Build FAILED.构建失败。

I stumbled upon this question and answer , and I noticed my local instance also had the same warning sign over the {Project Root} -> Dependencies -> npm folder.我偶然发现了这个问题和答案,我注意到我的本地实例在{Project Root} -> Dependencies -> npm文件夹上也有相同的警告标志。 Right clicking and hitting "Restore packages" got everything loaded up properly, and I was able to build successfully.右键单击并点击“恢复包”可以正确加载所有内容,并且我能够成功构建。

npm link webpack worked for me. npm link webpack 为我工作。

My webpack configuration: "webpack": "^4.41.2", "webpack-dev-server": "^3.9.0", "webpack-cli": "^3.3.10"我的 webpack 配置:“webpack”:“^4.41.2”、“webpack-dev-server”:“^3.9.0”、“webpack-cli”:“^3.3.10”

对于 Visual Studio 用户:右键单击 npm 文件夹和“Restore Packages”。

就我而言,帮助我更改了父文件夹名称并从此名称中删除了一些& ,您还可以尝试更改保存代码的名称或文件夹。

虽然建议的解决方案( npm link webpack )在本地工作,但在我的 CI(GitHub 操作)上我遇到了同样的问题,为了解决它,我使用了:

 npm i --save-dev webpack

Laravel Users Laravel 用户

If none of the above options work for you, then you probably need to install Laravel-mix correctly.如果以上选项都不适合你,那么你可能需要正确安装 Laravel-mix。 Here is how:方法如下:

npm install laravel-mix --save-dev

Now create a webpack.mix.js file using this command:现在使用以下命令创建一个webpack.mix.js文件:

touch webpack.mix.js

Add this code into your webpack.mix.js file:将此代码添加到您的webpack.mix.js文件中:

mix.js('resources/js/app.js', 'public/js').postCss('resources/css/app.css', 'public/css', [
    require('postcss-import'),
    require('tailwindcss'),
    require('autoprefixer'),
]);

You probably will also need to create a tailwind.config.js file using the command touch tailwind.config.js and then add this code ainto it:您可能还需要使用命令touch tailwind.config.js tailwind.config.js 创建一个tailwind.config.js文件,然后将以下代码添加到其中:


module.exports = {
    content: [
        './vendor/laravel/framework/src/Illuminate/Pagination/resources/views/*.blade.php',
        './storage/framework/views/*.php',
        './resources/views/**/*.blade.php',
    ],

    theme: {
        extend: {
            fontFamily: {
                sans: ['Nunito', ...defaultTheme.fontFamily.sans],
            },
        },
    },

    plugins: [require('@tailwindcss/forms')],
};

Finally run npm run dev最后运行npm run dev

So there are quite few possible issues, in my case on windows:所以几乎没有可能的问题,就我而言,在 Windows 上:

I moved my project to a folder with an & in the name, which is fine for windows but it break npm.我将我的项目移动到名称中带有&的文件夹中,这对 Windows 来说很好,但它破坏了 npm。 My solution was to remove the & from the name.我的解决方案是从名称中删除&

test&mocking -> test_and_mocking test&mocking模拟-> test_and_mocking

为我解决的问题是“build.js”中“webpack.config”的路径错误

rm -rf node_modules
rm -rf package.json-lock
npm install --force or npm install --legacy-peer-deps
rm -rf node_modules
rm -rf package-lock.json
npm install -f

After all the suggestions as a solution to the problem, simply deleting the "&" in the parent folder name did the trick.在所有建议作为问题的解决方案之后,只需删除父文件夹名称中的“&”就可以了。 Tnx Adam. Tnx 亚当。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM