Socket
Socket
Sign inDemoInstall

enforce-node-path

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

enforce-node-path

Enforce usage of NODE_PATH environmental variable to make loading application modules in Node.js more consistent.


Version published
Weekly downloads
87
increased by19.18%
Maintainers
1
Weekly downloads
 
Created
Source

Enforce NODE_PATH

Build Status Dependency Status

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",
  }
}

Keywords

FAQs

Package last updated on 14 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc