c0deBeastx
c0deBeastx

Reputation: 33

NodeJS module loads

Considering this is the file structure of my working directory

|-- bower.json
|-- lib
|   |-- foo1.js
|   |-- foo2.js
|   `-- foo3.js
|-- node_modules
|   |-- body-parser
|   |-- bower
|   |-- express
|   `-- md5
|-- package.json
|-- runserver.sh
|-- server.js
`-- test

How am I supposed to load the third party library modules ( present in ./node_modules ) in my modules that I write in ./lib directory ?

Upvotes: 0

Views: 73

Answers (1)

Your requires are relative to the file doing the requiring. If your server.js needs to require something from ./lib/, then you do that:

// in ./server.js
var foo1 = require('./lib/foo1'); // file path: resolve relative to this file.

The exception is "npm installed" dependencies, which live in the node_modules dir, and don't require a file location, just a name:

// in ./server.js
var express = require('express'); // not a file path: find in node_modules

// in ./lib/foo1.js
var express = require('express'); // not a file path: find in node_modules

// in some hypothetical ./lib/extended/secondary/mixin/foo7.js
var express = require('express'); // not a file path: find in node_modules

Upvotes: 1

Related Questions