node.js require cannot find custom module

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 but require('./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. 😅


The path is relative to the directory in which you are requireing 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.