node.js require cannot find custom module
Solution 1:
The path is relative to the directory in which you are require
ing the files, so it should be something like:
var couch = require('./couch');
var config = require('../config');
A bit of clarification, if you write
var couch = require('./couch');
you are trying to require
the couch module which resides in the current directory, if you write
var couch = require('couch');
you are trying to require
the couch module installed via npm
.
Solution 2:
The current accepted answer is correct and properly answers the question.
Although not directly related to the original question, I'd like to add another point here for all those people who got the Cannot find module
error for local files although the correct relative path was specified.
Assuming a file with the name couch.js
exists in the current directory,
-
On case insensitive filesystems (like NTFS on Windows), both these lines will work -
var couch = require('./couch'); var couch = require('./Couch');
-
However, on a case-sensitive filesystem (like ext4 on Linux),
require('./couch')
will work butrequire('./Couch')
will not.
There is a page in the NodeJS docs regarding this.
I hope this helps someone whose perfectly working code stopped working after moving from Windows/Mac to Linux. 😅
Solution 3:
Here is how you do it :
var users = require('./../modules/users');
Solution 4:
It must be:
var config = require(../../app/config)
var couch = require(./couch) (same directory)