enforce-node-path
v1.0.0
Published
Enforce usage of NODE_PATH environmental variable to make loading application modules in Node.js more consistent.
Downloads
308
Readme
Enforce NODE_PATH
Please Note: Due to the very limited scope of this module, I do not anticipate needing to make very many changes to it. Expect long stretches of zero updates—that does not mean that the module is outdated.
This simple module enforces usage of the NODE_PATH
environmental variable to make loading application modules (modules that are not in node_modules
) in Node.js more consistent.
Why?
The Node.js module loader uses a special environmental variable called NODE_PATH
:
If the
NODE_PATH
environment variable is set to a colon-delimited list of absolute paths, then Node.js will search those paths for modules if they are not found elsewhere.
This means that if you set the NODE_PATH
environmental variable to the root of your application, you can load application files relative to your root directory wherever you are.
Assuming that NODE_PATH
set to /var/www
:
// In /var/www/server/router.js
var routes = require('config/routes.json'); // Looks in /var/www/config/routes.json
Unfortunately, the module loader caches this environmental variable before your app runs, so it's not possible to set process.env.NODE_PATH
after the fact. And as the docs state, relying on this variable can be dangerous because developers may not know that it needs to be set. This module makes that dependency explicit and triggers an error if it's not set.
Installation
$ npm install enforce-node-path --save
Usage
As close to the top of your app's entry point, add the following code:
var enforceNodePath = require('enforce-node-path');
enforceNodePath(__dirname);
If NODE_PATH
is not set to __dirname
(path.resolve
is used, so they just have to resolve to the same directory), an Error
is thrown.
It is recommended that you set this environmental variable in your npm scripts section so that npm start
just works:
{
"scripts": {
"start": "NODE_PATH=. node index.js",
}
}