$ npm install root-require
a more convenient require method for certain situations
This lets you require()
using a relative path from the root directory of the present module.
Keep in mind
require()
is synchronous. And this library is not any more efficient-- (it usesfs.*Sync
methods) Just like when you userequire()
, you should be fine as long as you're doing this at the top of your file outside of any function declarations.
e.g. Consider trying to change the path to giggle.js
in an automated way:
hard
// foo.js
var Giggle = require('./wiggle/sniggle/giggle');
// bar.js
var Giggle = require('../../../../../wiggle/sniggle/giggle');
// baz.js
var Giggle = require('../../../../wiggle/sniggle/giggle');
// 20 more files like this, 100 other files like `giggle.js`
easy
// foo.js
var Giggle = require('root-require')('lib/wiggle/sniggle/giggle');
// bar.js
var Giggle = require('root-require')('lib/wiggle/sniggle/giggle');
// baz.js
var Giggle = require('root-require')('lib/wiggle/sniggle/giggle');
// 20 more files like this, 100 other files like `giggle.js`
Just once:
var Sails = require('root-require')('lib/app');
More than once:
var rootRequire = require('root-require');
var Sails = rootRequire('lib/app');
var Router = rootRequire('lib/router');
var MiddlewareLibrary = rootRequire('lib/middleware');
This is mainly a thin wrapper around packpath
(https://github.com/jprichardson/node-packpath)-- this module just exists so we can do it in one line.
MIT, c. 2014 Mike McNeil
© 2010 - cnpmjs.org x YWFE | Home | YWFE