hussainb
hussainb

Reputation: 1296

Nodejs : Require libraries from other files than server.js

This is my application folder structure for a node REST server.

MyApp
|
|__node_modules
|  |__lodash
|
|__routes
|  |__routes.js
|
|__server.js

In server.js I am able to require the loadsh library by doing

var _l = require("lodash");

(Problem 1)

But the variable _l is not accessible in the code of routes.js.

(Problem 2)

So I tried to require lodash from my routes.js, but node isnt able to resolve path to it. I tried adding "./" , "../" but it doesnt work.

So, is there any way if the var _l create in server.js be accessible everywhere else, and if not , what is the proper way to include modules in files(not in root folder) other than server.js

Upvotes: 0

Views: 99

Answers (1)

Zoey Mertes
Zoey Mertes

Reputation: 3144

Just var _l = require("lodash");.

Node will search for modules recursively, all the way to /node_modules or C:\node_modules

Upvotes: 1

Related Questions