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
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 is under
utils.js is under
I tried below require combinations, but still I am getting cannot find module error:
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?
I tried putting changing my folder structure as pointed out by @mithunsatheesh as below:
- utils – utils.js
require is as follows:
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
This is the case if you have your project folder structured like
and you are doing
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 the
projfolder with your project files then the mentioned require will be an error.
Also the folder structure could be made to something like
- utils – utils.js
so that you keep the main file in root of project folder. and require the utils.js like
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 as
proj folder, see that the proposed folder structure had
src folder in same level inside
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.