Chris Deacy
Chris Deacy

Reputation: 265

Having trouble requiring local module

I am using a file structure similar to this:

-Projects
    -a
        -package.json
        -lib
            -index.js
    -b
        -package.json
        -lib
            -index.js

The package.json file within both a and b contains:

"main": "lib"

Within the a index.js file, I am trying to use:

var b = require('../b');

and getting the error:

Error: Cannot find module '../b'

Am I completely wrong in trying to require it this way? What am I missing? Any help is appreciated!

Upvotes: 0

Views: 35

Answers (1)

DevAlien
DevAlien

Reputation: 2476

If you try to require the module with a path, it will search for that file and not a module. what you want is, in the package.json of a is this

"dependencies": {
   "b": "file:../b"
}

then in a index.js you can call it this way var b = require('b');

Upvotes: 1

Related Questions