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
- Host: GitHub
- URL: https://github.com/silvae86/rlequire
- Owner: silvae86
- License: mit
- Created: 2018-05-10T17:57:48.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-04-09T16:15:38.000Z (about 7 years ago)
- Last Synced: 2025-11-03T11:28:56.166Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 78.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/silvae86/rlequire)
[](https://www.codacy.com/app/silvae86/rrequire?utm_source=github.com&utm_medium=referral&utm_content=silvae86/rrequire&utm_campaign=Badge_Grade)
[](https://www.codacy.com/app/silvae86/rrequire?utm_source=github.com&utm_medium=referral&utm_content=silvae86/rrequire&utm_campaign=Badge_Coverage)
[](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`.