0

I have just started learning node and express and stumbled across this sample express application - https://github.com/madhums/node-express-mongoose-demo. The server.js file loads a module named config without a relative path -

var config = require('config')

https://github.com/madhums/node-express-mongoose-demo/blob/master/server.js#L15. What I don't understand is how the module is getting loaded. There is no module named config in node_modules. The package.json contains NODE_PATH which points to ./config folder. However, the config folder doesn't contain an index.js file. So how does the config object gets its properties?

1 Answer 1

2

NODE_PATH points to super-folders that contain Node modules (which may themselves be in folders, or maybe single files).

The modules documentation says:

If the NODE_PATH environment variable is set... then node will search those paths for modules if they are not found elsewhere.

It search those paths for modules; it does not treat those paths as modules.

Therefore, require('config') loads ./config/config.js. The NODE_PATH=./config:... tells Node to look in ./config for modules, and it find successfully finds a file called config.js in that folder, which it loads as a module.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.