简体   繁体   中英

Custom module in nodejs returns “cannot find module” error

I've been writing a little prototype app in the cloud9 ide using express and nodejs.

I was going to re-organize some code into a few custom modules but when I attempt to require them I get an error "cannot find module './lib'

The lib folder is in the root of my site directory. I have 1 file in there "test.js" that has a simple demo to try and get this working.

exports.test = function(){
  return 'hello world';  
};

It doesn't matter if I put require('./lib') or require('./lib/test') in my app.js, it always throws the same error.

Any thoughts?

Two potential errors:

1- require() looks for a file called index.js. So try renaming test.js to index.js might work.

2- Your path is wrong. Is the lib folder in the same folder as the file in which you call the require()?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM