1.0.0 • Published 7 years ago

node-root.pddivine v1.0.0

Weekly downloads
3
License
BSD-2-Clause
Repository
github
Last release
7 years ago

node-root.pddivine

Easy, lightweight determiner of the project root full path to be dropped into any node module.

TLDR;

const root = require('node-root.pddivine') // /path/to/node/application/root/directory

const yourModule = require(`${root}/relative/path/to/yourModule`); // Success!

How It Works

At runtime node creates a registry of the full paths of all loaded files. The modules are loaded first, and thus at the top of this registry. By selecting the first element of the registry and returning the path before the 'node_modules' directory we are able to determine the root of the application.

The entirety of the code is as follows:

module.exports = process.mainModule.paths[0].split('node_modules')[0].slice(0, -1); // /path/to/node/application/root/directory

Other Notes

  • This will work even if there are no node modules. ( But if you use this library you will have at least one ;) )
  • This will work with test frameworks, like Mocha.

Concluding Remarks

Enjoy!