如何使用 Node.js 解决“找不到模块”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9023672/
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
How do I resolve "Cannot find module" error using Node.js?
提问by Dave Causey
After pulling down a module from GitHub and following the instructions to build it, I try pulling it into an existing project using:
从 GitHub 中提取模块并按照说明进行构建后,我尝试使用以下方法将其提取到现有项目中:
> npm install ../faye
This appears to do the trick:
这似乎可以解决问题:
> npm list
/home/dave/src/server
└─┬ [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
But Node.js can't find the module:
但是 Node.js 找不到模块:
> node app.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'faye'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Object.<anonymous> (/home/dave/src/server/app.js:2:12)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:470:10)
I really want to understand what is going on here, but I'm at a bit of a loss as to where to look next. Any suggestions?
我真的很想了解这里发生了什么,但我不知道下一步该往哪里看。有什么建议?
采纳答案by Bill
Using npm installinstalls the module into the current directory only (in a subdirectory called node_modules). Is app.js located under home/dave/src/server/? If not and you want to use the module from any directory, you need to install it globally using npm install -g.
Using 仅npm install将模块安装到当前目录中(在名为 的子目录中node_modules)。app.js 是否位于home/dave/src/server/? 如果没有,并且您想使用任何目录中的模块,则需要使用npm install -g.
I usually install most packages locally so that they get checked in along with my project code.
我通常在本地安装大多数软件包,以便它们与我的项目代码一起被检入。
Update (8/2019):
更新(8/2019):
Nowadays you can use package-lock.jsonfile, which is automatically generated when npm modifies your node_modules directory. Therefore you can leave out checking in packages, because the package-lock.jsontracks the exact versions of your node_modules, you're currently using. To install packages from package-lock.jsoninstead of package.jsonuse the command npm ci.
现在你可以使用package-lock.json文件,当 npm 修改你的 node_modules 目录时它会自动生成。因此,您可以省略签入包,因为它会package-lock.json跟踪您当前正在使用的 node_modules 的确切版本。要从而package-lock.json不是package.json使用命令安装软件包npm ci。
Update (3/2016):
更新 (3/2016):
I've received a lot of flak for my response, specifically that I check in the packages that my code depends on. A few days ago, somebody unpublished all of their packages (https://kodfabrik.com/journal/i-ve-just-liberated-my-modules) which broke React, Babel, and just about everything else. Hopefully it's clear now that if you have production code, you can't rely on NPM actually maintaining your dependencies for you.
我的回复收到了很多批评,特别是我签入了我的代码所依赖的包。几天前,有人取消发布了他们所有的包(https://kodfabrik.com/journal/i-ve-just-liberated-my-modules),这破坏了 React、Babel 和其他几乎所有内容。希望现在很清楚,如果你有生产代码,你不能依赖 NPM 实际上为你维护你的依赖项。
回答by Abhinav Singh
I had a very similar issue. Removing the entire node_modulesfolder and re-installing worked for me:
我有一个非常相似的问题。删除整个node_modules文件夹并重新安装对我有用:
rm -rf node_modules
npm install
回答by Piyush Chordia
npm install --save module_name
For example, if the error is:
例如,如果错误是:
{ [Error: Cannot find module '/root/.npm/form-data'] code: 'MODULE_NOT_FOUND' }
{ [错误:找不到模块 '/root/.npm/form-data'] 代码:'MODULE_NOT_FOUND' }
then you can resolve this issue by executing the command npm install --save form-data.
那么你可以通过执行命令来解决这个问题npm install --save form-data。
回答by mgthomas99
For TypeScript users, if you are importing a built-in Node module (such as http, pathor url) and you are getting an error such as "Cannot find module "x"then the error can be fixed by running
对于 TypeScript 用户,如果您正在导入一个内置的 Node 模块(例如http,path或url)并且您收到诸如此类"Cannot find module "x"的错误,则可以通过运行来修复该错误
npm install @types/node --save-dev
The command will import the NodeJS TypeScript definitions into your project, allowing you to use Node's built-in modules.
该命令会将 NodeJS TypeScript 定义导入到您的项目中,从而允许您使用 Node 的内置模块。
回答by Alex Werner
This happens when a first npminstall has crashed for some reason (SIGINT of npm), or that the delay was too long, or data is corrupted. Trying an npm install again won't save the problem.
当第一个npm安装由于某种原因(npm 的 SIGINT)崩溃,或者延迟太长或数据损坏时,就会发生这种情况。再次尝试 npm install 不会解决问题。
Something got wrong on the npm first check, so the best choice is to remove the file and to restart npm install.
npm first check 出了点问题,所以最好的选择是删除文件并重新启动 npm install。
回答by Ilya Sheershoff
If you use nvm, check that existing node_modules that are bindings to other libraries are compiled for the correct Node.js version.
如果您使用 nvm,请检查绑定到其他库的现有 node_modules 是否已针对正确的 Node.js 版本进行编译。
I was having the same error. The reason was the following: We use nvm since we're running two apps on a server, one requires Node.js 5.6 because it uses node-gd (which doesn't run on Node.js 6 for now), the other requires Node.js 6. Node.js 6 is the apt-getinstallation.
我有同样的错误。原因如下:我们使用 nvm,因为我们在服务器上运行两个应用程序,一个需要 Node.js 5.6,因为它使用 node-gd(目前不在 Node.js 6 上运行),另一个需要Node.js 6. Node.js 6 是apt-get安装。
Also we use the pm2 tool to deploy.
我们也使用 pm2 工具进行部署。
So, the default setup is that the pm2 process starts when nvm is not in effect, so it uses the apt-get installation of Node.js (version 6). So the main pm2 daemon starts with Node.js 6. If I run applications in fork mode they start in separate processes and nvm settings are in effect. When I run applications in cluster mode - they inherit the non-nvm environment.
所以,默认设置是在 nvm 没有生效时启动 pm2 进程,所以它使用 Node.js(版本 6)的 apt-get 安装。所以主要的 pm2 守护进程从 Node.js 6 开始。如果我在 fork 模式下运行应用程序,它们会在单独的进程中启动并且 nvm 设置生效。当我在集群模式下运行应用程序时 - 它们继承了非 nvm 环境。
So when I tried to switch to the cluster mode the application failed to start because the bindings compiled for 5.6 fail with this message.
因此,当我尝试切换到集群模式时,应用程序无法启动,因为为 5.6 编译的绑定失败并显示此消息。
I've fixed that by restarting pm2 when nvm setings are in effect. Also startup scripts should be fixed.
我通过在 nvm 设置生效时重新启动 pm2 来解决这个问题。还应该修复启动脚本。
回答by Edgar Chavolla
Check if the enviroment variable NODE_PATH is set correctly and pointing to the node_modules path. nodejs uses this variable to search for the libraries
检查环境变量 NODE_PATH 是否设置正确并指向 node_modules 路径。nodejs 使用这个变量来搜索库
回答by ukosteopath
I experienced this error yesterday. Took me a while to realise that the mainentry in package.jsonwas pointing to a file that I'd moved. Once I updated that the error disappeared and the package worked.
我昨天遇到了这个错误。我花了一段时间才意识到main输入的条目package.json指向我移动的文件。一旦我更新错误消失并且程序包工作。
回答by MrCranky
This error can be encountered if you are requireing a module that has a missing or incorrect mainfield in its package.json. Though the module itself is installed, npm/node has to use a single .js file as an entrypoint to your module. If the mainfield is not there, it defaults to looking for index.jsin your module's folder. If your module's main file is notcalled index.js, it won't be able to requireit.
如果您正在require调用的模块main在 package.json 中缺少或不正确的字段,则可能会遇到此错误。尽管模块本身已安装,但 npm/node 必须使用单个 .js 文件作为模块的入口点。如果该main字段不存在,则默认为index.js在您的模块文件夹中查找。如果你的模块的主文件不是index.js,它就不能require。
Discovered while turning a browserify-based module into a CommonJS require-able module; browserifydidn't care about the missing mainfield, and so the error had gone unnoticed.
在将-based 模块browserify转换为 CommonJS require-able 模块时发现;browserify不关心缺失的main字段,所以错误没有被注意到。
回答by PrashanthiDevi
Remove your node_moduleroot folder from your project(eg: myApp).
Go to myAppfolder and then type below command from terminal
node_module从您的项目中删除您的根文件夹(例如:)myApp。转到myApp文件夹,然后从终端输入以下命令
>myApp>npm install
It will install all the dependency modules required for your project.
它将安装您的项目所需的所有依赖模块。

