Get unlimited public & private packages + package-based permissions with npm Pro.Get started »

root_require

0.0.0 • Public • Published

root_require

Hijacks node's module resolution so that you can use a prefixed path to require modules from the project root.

If you want to accomplish this by transforming your code rather than using this runtime node hack, or for bundling for the browser, use babel-plugin-root-require.

install

$ npm install root_require

example

require('root_require').setRoot()
 
const myThing = require('~/things/relatively')

Starting from your project root, if you have a file lib/foo.js and you want to require it from routes/user/profile, you would normally write require('../../../lib/foo'). With root_require initialized, you can just write require('~/lib/foo') from anywhere.

api

setRoot(pathToRoot)

If no argument is given, setRoot will look for a package.json and consider the directory it is found in to be the root.

setPrefix(prefix)

The prefix to use to refer to the project root.

require('root_require').setPrefix('TATERS').setRoot()
 
require('TATERS/foo.js')

reset()

Restore module resolution to default behavior. You probably don't ever need to do this.

Install

npm i root_require

DownloadsWeekly Downloads

1

Version

0.0.0

License

CC0-1.0

Last publish

Collaborators

  • avatar