Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

root-require

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

root-require

For use in tests only! require() using a relative path from the root directory of the present module

  • 0.3.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.6K
decreased by-25.45%
Maintainers
1
Weekly downloads
 
Created
Source

root-require

a more convenient require method for running TESTS ONLY.

WARNING:

THIS MODULE PROBABLY DOES NOT WORK IN THE WAY YOU EXPECT IT TO WORK ALL THE TIME. I REPEAT: IT HAS DIFFERENT BEHAVIOR BASED ON HOW IT'S BROUGHT IN AS A DEPENDENCY THAT WILL CAUSE YOU HEADACHES. ESPECIALLY IF YOU USE IT IN TWO MODULES WHICH DEPEND ON ONE ANOTHER. (this is because of the way npm optimizes dependencies, and this module doesn't address that, since it's for testing only.).

That said, as long as you use this module as a DEV DEPDENDENCY, everything should work as expected. Sorry for the capslock. ~Mike

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.

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

Why is this a good thing?

It's easier to reason about the structure of your module when the paths are consistent. The structure of your project becomes more declarative- dependencies are consistently referenced, irrespective of the user file's home in the directory structure.

Problems w/ require()
  1. When you move a dependency file (Ax) required by multiple files (Bi), you have to find/replace the all references to Ax. This is normally hard, because the argument to the require(...) function depends on where the user file (Bi) is located.
  2. When you move a file (Bx) which depends on another file (Ax), you normally have to update the require() call in Bx to reflect the new relative path from Bx to Ax.

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`

Credit where credit is due

This module is literally a 3-line wrapper around the awesome packpath module (https://github.com/jprichardson/node-packpath). I just made this for convenience/ so I could have it in one line because I always forget how path.join works w/ Windows and all that.

License

MIT, c. 2014 Mike McNeil

Keywords

FAQs

Package last updated on 15 Jan 2014

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