在 node.js 中找不到模块错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16163176/
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
Module not found error in node.js
提问by Pradeep Simha
I am new to node.js, this is my first node application so, please excuse me if I'm asking obvious question. I have a file called utils.jsand I need to have functions defined in that file to be available in main.js. So I tried giving
我是 node.js 的新手,这是我的第一个 node 应用程序,所以如果我问明显的问题,请原谅。我有一个调用的文件utils.js,我需要在该文件中定义函数才能在main.js. 所以我试着给
require(utils.js)
require(utils.js)
But it is throwing me this error:
但它向我抛出了这个错误:
Error: Cannot find module 'utils.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
My main.jsis under c:\demo\proj\src\main\main.jsand utils.jsis under c:\demo\proj\src\utils\utils.js.
我main.js的在下c:\demo\proj\src\main\main.js,utils.js在下c:\demo\proj\src\utils\utils.js。
I tried below require combinations, but still I am getting cannot find module error:
我在下面尝试了需要组合,但仍然无法找到模块错误:
require(/proj/src/utils/utils.js);require(/utils.js);require(c:/demo/proj/src/utils/utils.js);
require(/proj/src/utils/utils.js);require(/utils.js);require(c:/demo/proj/src/utils/utils.js);
Even I tried to put it under node_modulesfolder, but still same error. Can you kindly guide me what I am doing mistake here?
即使我试图把它放在node_modules文件夹下,但仍然是同样的错误。你能指导我在这里做错什么吗?
Edit:
编辑:
I tried putting changing my folder structure as pointed out by @mithunsatheesh as below:
我尝试按照@mithunsatheesh 指出的那样更改我的文件夹结构,如下所示:
- project
- src
- utils - utils.js
- src
- main.js
- 项目
- 源文件
- utils - utils.js
- 源文件
- 主文件
My requireis as follows: require('./src/utils/utils.js')
我require的如下:require('./src/utils/utils.js')
But when I execute node main.jsstill I am getting below error:
但是当我node main.js仍然执行时,我收到以下错误:
Error: Cannot find module './src/utils/utils.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
采纳答案by mithunsatheesh
according to the folder structure you mentioned in the question, you have to try
根据您在问题中提到的文件夹结构,您必须尝试
require('../utils/utils.js')
This is the case if you have your project folder structured like
如果您的项目文件夹结构如下
- proj
- src
- utils
- utils.js
- main
- main.js
- utils
- src
- 项目
- 源文件
- 实用程序
- 实用程序.js
- 主要的
- 主文件
- 实用程序
- 源文件
and you are doing node main.js
你正在做 node main.js
To comment on the details provided in your question.
评论您的问题中提供的详细信息。
please dont use
require(c:/demo/proj/src/utils/utils.js);as you are tried out. imagine like you are exporting theprojfolder with your project files then the mentioned require will be an error.Also the folder structure could be made to something like
- proj
- src
- utils - utils.js
- main.js
- package.json
- src
- proj
请不要使用,
require(c:/demo/proj/src/utils/utils.js);因为您正在试用。想象一下,就像您正在导出proj带有项目文件的文件夹,那么提到的 require 将是一个错误。文件夹结构也可以做成类似的东西
- 项目
- 源文件
- utils - utils.js
- 主文件
- 包.json
- 源文件
- 项目
so that you keep the main file in root of project folder. and require the utils.js like
以便您将主文件保存在项目文件夹的根目录中。并需要 utils.js 之类的
require('./src/utils/utils.js')
UPDATE
更新
As far as ican see from the updated error message. Its still the issue with the path of 'utils.js' in require. From your updated folder structre It seems that main.jsis in same level as projfolder, see that the proposed folder structure had main.jsand srcfolder in same level inside projfolder.
从更新的错误消息中可以看出。它仍然是require中'utils.js'路径的问题。从更新后的文件夹结构法似乎main.js是在同一级别proj的文件夹,看到建议的文件夹结构必须main.js与src在内部同级别的文件夹proj的文件夹。
Even that was a suggestion that I made as you were following a folder structure that dint make any sense. Simply require('../utils/utils.js')would have solved your issue without even altering the folder structure you mentioned in the beginning.
即使这是我提出的建议,因为您正在遵循一个有意义的文件夹结构。简单地require('../utils/utils.js')将已经解决您的问题,甚至没有改变你在开头提到的文件夹结构。
回答by Minko Gechev
Use: require('./utils.js');. This is the correct way to require a module from a file which is located in required module's folder.
用途:require('./utils.js');。这是从位于所需模块文件夹中的文件中要求模块的正确方法。
You must provide a relative or absolute path I guess utils.jsis not in your root so require('/utils.js');is not the right path.
您必须提供一个相对或绝对路径,我猜utils.js它不在您的根目录中,因此require('/utils.js');不是正确的路径。
Example:
例子:
Imagine you have two files utils.jsand main.jsin the same folder. The content of utils.jsis:
想象一下,您有两个文件utils.js并且main.js位于同一个文件夹中。内容utils.js为:
utils.js
实用程序.js
exports.foo = function () {
console.log('foo');
};
In order to call foofrom utils.jsin main.jsyou should use:
为了foo从utils.jsin调用,main.js您应该使用:
main.js
主文件
require('./utils.js').foo();

