70

Here is the project structure:

/
  app.js
  package.json
  /node_modules
  /app
    config.json
    /frontend
      assets and html tpls
    /modules
      couch.js
      raeume.js
      users.js

I require config.json, raeume.js and users.js from app.js and it all works fine.

var config   = require('./app/config');
var raeume   = require('./app/modules/raeume');
var users    = require('./app/modules/users');

Then I require config.json and couch.js from user.js the same way and it won't find anything.

var couch     = require('./app/modules/couch');
var config    = require('./app/config');

I guess it should find it. After some research I saw a diverse landscape of problems inclusive how node is compiled. Thus included: I work on osx 10.8 with node v0.10.7.

thgie
  • 2,367
  • 1
  • 18
  • 28

5 Answers5

142

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.

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
  • I try relative: var couch = require('couch'); var config = require('../config'); finds config but not couch. – thgie May 20 '13 at 15:22
  • 2
    You missed the `./` in `require('couch');` – Alberto Zaccagni May 20 '13 at 15:27
  • I doesn't work with me: when I do either `require('qmlweb')` or `require('./node_modules/qmlweb')`, it says the module cannot be found. Yet qmlweb is installed in node_modules/qmlweb, and all the files are there. – Michael Feb 24 '16 at 11:11
  • Please open a new question posting your relevant information, such as a snippet with your code and the likes, link it here if you prefer. – Alberto Zaccagni Feb 24 '16 at 11:18
4

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.

ManSamVampire
  • 153
  • 2
  • 6
2

Here is how you do it :

var users    = require('./../modules/users');
Dory Zidon
  • 10,497
  • 2
  • 25
  • 39
1

It must be:

var config = require(../../app/config)

var couch = require(./couch) (same directory)
Nghiep Ngo
  • 27
  • 1
0

Also double check the extension on the file being imported. I accidentally used .ts in a JavaScript project, and this prevented it from being found. Once I changed the extension to .js, require was able to find it fine.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117