Socket
Socket
Sign inDemoInstall

root-require

Package Overview
Dependencies
1
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    root-require

require() using a relative path from the root directory of the present module


Version published
Weekly downloads
5.2K
decreased by-1.6%
Maintainers
1
Install size
11.1 kB
Created
Weekly downloads
 

Readme

Source

root-require

a more convenient require method for certain situations

How it do

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 uses fs.*Sync methods) Just like when you use require(), you should be fine as long as you're doing this at the top of your file outside of any function declarations.

Use Cases

  1. A file needs to move, and you want to easily be able to find/replace the references to it.

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`

Usage

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');

Credit where credit is due

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.

License

MIT, c. 2014 Mike McNeil

Keywords

FAQs

Last updated on 03 Jan 2014

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc