An open API service indexing awesome lists of open source software.

https://github.com/silvae86/rlequire

"Relative Require": No more relative require hell! A nodejs module for requiring files in paths relative to one where a specific package.json is located
https://github.com/silvae86/rlequire

Last synced: 4 days ago
JSON representation

"Relative Require": No more relative require hell! A nodejs module for requiring files in paths relative to one where a specific package.json is located

Awesome Lists containing this project

README

          

[![Build Status](https://travis-ci.org/silvae86/rlequire.svg?branch=master)](https://travis-ci.org/silvae86/rlequire)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/cff4cfde36144314adbdce30226737e6)](https://www.codacy.com/app/silvae86/rrequire?utm_source=github.com&utm_medium=referral&utm_content=silvae86/rrequire&utm_campaign=Badge_Grade)
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/cff4cfde36144314adbdce30226737e6)](https://www.codacy.com/app/silvae86/rrequire?utm_source=github.com&utm_medium=referral&utm_content=silvae86/rrequire&utm_campaign=Badge_Coverage)
[![npm version](https://badge.fury.io/js/rlequire.svg)](https://badge.fury.io/js/rlequire)

# rlequire
Tired of require("../../../../../whatever") ????

"Relative Require": A nodejs module for requiring files in paths relative to one where a specific package.json is located.

## Why is this needed?

Because NodeJS does not have a robust way of determining the root path of a running application. Sure, when your app is a couple of files you dont have problems, but as it starts growing, with modular parts and plugins, you want to be sure you can locate the actual root of your application. Also, mocha makes your `__basedir` stop working.

https://stackoverflow.com/questions/10265798/determine-project-root-from-a-running-node-js-application

## How to use?

````bash
npm install rlequire --save
````

````node
const rlequire = require("rlequire")
const Client = rlequire("your-app", "./src/model/client.js", [optional, boolean] forceRecheck).Client;
````

rrequire will search the current folder of the file from which it is invoked and the entire upstream directory until the root of the filesystem. Whenever a `package.json` file is detected, it will attempt to parse it.

- If the `name` field in the package.json matches `your-app`, it will detect that folder as the root of the app.
- If the file path `"./src/model/client.js"` exists, relatively to the detected root folder, it will require the file for you.
- The expensive directory traversal operation is done only the first time we need to determine the root of an app, unless the `forceRescan` optional argument is specified as `true`.