Bryce Johnson
Bryce Johnson

Reputation: 6901

Why can't Node resolve this filename?

I'm getting the following error with an app I'm building in node.

Error: Cannot find module '/Users/bryce/repos/wudu/requester'

This occurs when I try to access a module (requester/requests.js) from this file (main.js):

main.js

var r = require('./requester/requests');

r.updateDB();

I am perplexed as to why the error message cuts off the filename, and refers to the directory containing the file.

I am also perplexed because I get the same error when I try to execute the file (requester/requests.js) on its own.

Here is a simplified version of what is contained in the file, if that helps.

requester/requests.js

var $ = require('cheerio');
var request = require('request');
var q = require('q');
var dbStatus = require('./dbStatus');

function updateDB() {
    // n/a
}


module.exports = {
    updateDB: updateDB
};

The app's folder contains main.js and requester both. So I'm fairly certain my path declarations are correct. But I am fairly new to Node, so it's possible that I'm misunderstanding how modules work.

Thanks in advance!

Upvotes: 0

Views: 2520

Answers (1)

thtsigma
thtsigma

Reputation: 4938

The only way I can recreate your error is if the main.js is in the same directory as the requestor.js. In order for your require call to work properly, your directory structure should look like so:

computer@computer:~/node_test$ tree
.
├── main.js
└── requester
  └── requests.js

1 directory, 2 files

If your directory looks like this:

computer@computer:~/node_test$ tree
.
└── requester
  └── main.js
  └── requests.js

or like this:

computer@computer:~/node_test$ tree
.
└── main.js
└── requests.js

then you would need to modify your main.js to look like so:

var r = require('./requests');

r.updateDB();

Hopefully this solves your problem, if not I'll be happy to take another look at it. If you could edit your OP with the tree printout of your directory that would be helpful.

----- Edit -----

It was asked if a file permission error could cause this same type of problem. Here's an example of that:

@computer:~/node_test$ sudo chown root:root requester/requests.js 
@computer:~/node_test$ sudo chmod 700 requester/requests.js 
@computer:~/node_test$ node main.js 

fs.js:338
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
             ^
Error: EACCES, permission denied './node_test/requester/requests.js'

So in that case where the file itself has a permission problem it throws a different error; however, if we make the folder only accessible to root then you'd have this going on:

@computer:~/node_test$ sudo chown root:root requester/
@computer:~/node_test$ sudo chmod 700 requester/
@computer:~/node_test$ node main.js 

module.js:340
  throw err;
      ^
Error: Cannot find module './requester/requests'

So as @akaphenom correctly pointed out, you may have a permission problem with the ./requester folder. You can view the permissions with the following:

@computer:~/node_test$ ls -l

Good luck resolving your problem.

Upvotes: 3

Related Questions