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)
My main.js
is under c:\demo\proj\src\main\main.js
and utils.js
is under 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
);
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?
Edit:
I tried putting changing my folder structure as pointed out by @mithunsatheesh as below:
- project
- src
- utils - utils.js
- src
- main.js
My require
is as follows: require('./src/utils/utils.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)
require('./utils');
?