0

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

1 Answer 1

1

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

So it means there was no need to try "../" ... Ohhh
Technically you can do that, by require('../node_modules/lodash');, but that's stupid and bad form.
Yes. It works directly, I am wondering why it erred the first time I tried directly. It made me ask a question! but , thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.