23

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
  • 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)
2
  • Did you try require('./utils'); ? Commented Apr 23, 2013 at 7:15
  • @limelights, yes I tried but still getting same error. I tried to put utils.js in the same location as main.js but no luck. Commented Apr 23, 2013 at 7:17

3 Answers 3

9

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

and you are doing node main.js

To comment on the details provided in your question.

  1. please dont use require(c:/demo/proj/src/utils/utils.js); as you are tried out. imagine like you are exporting the proj folder with your project files then the mentioned require will be an error.

  2. Also the folder structure could be made to something like

    • proj
      • src
        • utils - utils.js
      • main.js
      • package.json

so that you keep the main file in root of project folder. and require the utils.js like

 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.js is in same level as proj folder, see that the proposed folder structure had main.js and src folder in same level inside proj folder.

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.

2
  • @PradeepSimha if you have tried out the steps that Minko Gechev and me pointed out, i believe the error that you get in console should not be cannot find module utils.js . So consider updating your question with the exact errors and the exact code you are using now. Commented Apr 23, 2013 at 8:38
  • I don't know how this is the answer because I have a similar issue stackoverflow.com/questions/40963845/… Commented Dec 4, 2016 at 21:28
4

Use: require('./utils.js');. This is the correct way to require a module from a file which is located in required module's folder.

You must provide a relative or absolute path I guess utils.js is not in your root so require('/utils.js'); is not the right path.

Example:

Imagine you have two files utils.js and main.js in the same folder. The content of utils.js is:

utils.js

exports.foo = function () {
    console.log('foo');
};

In order to call foo from utils.js in main.js you should use:

main.js

require('./utils.js').foo();
7
  • Did you mean, I should put it under node_modules folder? Commented Apr 23, 2013 at 7:18
  • @PradeepSimha : no it should be in same level as your main.js - which i guess is the node.js file you are running Commented Apr 23, 2013 at 7:21
  • Thanks and I tried same, I put both main.js and utils.js in same folder, still I am getting same error :( Commented Apr 23, 2013 at 7:25
  • Do you use the same require parameter ./utils.js notice that I have . before the slash it's very important! Commented Apr 23, 2013 at 7:27
  • I have tried like this var s = require('./utils.js').Foo(); both utils.js and main.js are in same folder now. Commented Apr 23, 2013 at 7:28
1

First of all, check your current folder. Are you in the right folder?

if(yes) then this answer is not for you can leave and go further

if(not sure || No)

cd "your folder name." 
node "your file name.js."
1
  • you should better format the answer and use some Markdown formatting like code bold etc. - use Help/Preview button. Commented Apr 15, 2021 at 9:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.