问题描述
我是node.js的新手,这是我的第一个节点应用程序,因此,如果我要提出明显的问题,请原谅.我有一个名为 utils.js
的文件,我需要在该文件中定义函数才能在 main.js
中使用.所以我尝试给
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.js
and I need to have functions defined in that file to be available in main.js
. So I tried giving
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)
我的 main.js
在 c:\ demo \ proj \ src \ main \ main.js
下,而 utils.js
在 c:\ demo \ proj \ src \ main \ main.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
);
即使我尝试将其放在 node_modules
文件夹下,但仍然存在相同的错误.您能指导我在这里做错什么吗?
Even I tried to put it under node_modules
folder, but still same error. Can you kindly guide me what I am doing mistake here?
我尝试按照@mithunsatheesh指出的那样更改文件夹结构,如下所示:
I tried putting changing my folder structure as pointed out by @mithunsatheesh as below:
- 项目
- src
- utils-utils.js
我的
require
如下:require('./src/utils/utils.js')
但是当我执行
node main.js
时,仍然出现以下错误:But when I execute
node main.js
still I am getting below error: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)
推荐答案
根据您在问题中提到的文件夹结构,您必须尝试
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
- 项目
- src
- 实用程序
- utils.js
- main.js
,您正在做
node main.js
评论您的问题中提供的详细信息.
To comment on the details provided in your question.
-
请不要尝试使用
require(c:/demo/proj/src/utils/utils.js);
.想象一下,就像您正在导出带有项目文件的proj
文件夹一样,那么上述需求将是一个错误.
please dont use
require(c:/demo/proj/src/utils/utils.js);
as you are tried out. imagine like you are exporting theproj
folder with your project files then the mentioned require will be an error.
也可以将文件夹结构做成类似
Also the folder structure could be made to something like
- 项目
- src
- 实用程序-utils.js
,以便将主文件保留在项目文件夹的根目录中.并要求像
so that you keep the main file in root of project folder. and require the utils.js like
require('./src/utils/utils.js')
更新
据我从更新的错误消息中看到的.仍然是require中"utils.js"路径的问题.从更新后的文件夹structre中,看来
main.js
与proj
文件夹处于同一级别,请参阅所建议的文件夹结构具有main.js
和proj
文件夹中同一级别的src
文件夹.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.js
is in same level asproj
folder, see that the proposed folder structure hadmain.js
andsrc
folder in same level insideproj
folder.即使这是我在您遵循一个没有任何意义的文件夹结构时提出的建议.只需
require('../utils/utils.js')
就可以解决您的问题,甚至无需更改开始时提到的文件夹结构.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.这篇关于在node.js中找不到模块错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- src
- 实用程序
- src
- src