Skip to content

marc136/node-require-upwards

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-require-upwards

Require code somewhere upwards in the file tree without knowing the specific number of parent folders needed.
The tree is traversed synchronously.

Motivation/Usage

Instead of writing code like this:

try {
  var abc = require('./abc.js');
} catch (ex) {
  try {
    abc = require('../../abc.js');
  } catch (err) {
    abc = require('../../../abc.js');
  }
}

I rather want to write something like this:

var requireUp = require('require-upwards');
var abc = requireUp('abc.js');

Limiting the number of checked folders

Version 1.1 adds an optional parameter to limit the maximum number of iterations.

Given this folder structure:

fixtures
 ├─fix.js
 └─11
   ├─a.js
   └─baum

When starting from the folder fixtures/11/baum

requireUp('fix.js', 1)
# will throw an Error
requireUp('fix.js', 2)
# will return the content of fixtures/fix.js

Tests

Unit tests are located in the folder ./test.
To run them, execute

npm run test --silent

About

node.js: require code somewhere upwards in the file tree

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published