pquire
A simple module for better local requiring
Installation
npm i -S pquire
Typical Usage
File structure:
<root>
┣╸lib
┃ ┣╸settings.js
┃ ┗╸util.js
┣╸src
┃ ┣╸module1.js
┃ ┣╸duplicate.js
┃ ┗╸mod2.js
┣╸duplicate.js
┗╸index.js
Documentation
Function | Description |
---|---|
pquire(<path>) |
First tries to require relative to the current file, then, if that fails, requires relative to the package root. |
pquire.abs(<path>) |
Forces the require path to be relative to the root of the project. |
pquire.rel(<path>) |
Forces the require path to be relative to the path of the current file. This is the same as running require(./<path>) |
pquire.withBaseRelative(<path>) |
Creates a new pquire instance that uses <path> as its absolute base instead of getting it dynamically. This differs from withBaseAbsolute in that it interprets <path> relative to the current file. |
pquire.withBaseAbsolute(<path>) |
Creates a new pquire instance that uses <path> as its absolute base instead of getting it dynamically. This differs from withBaseRelative in that it interprets <path> relative to the project root. |
Examples
File structure:
<root>
┣╸lib
┃ ┣╸settings.js
┃ ┗╸util.js
┣╸src
┃ ┣╸module1.js
┃ ┣╸duplicate.js
┃ ┗╸mod2.js
┣╸duplicate.js
┗╸index.js
Typical usage:
<root>/src/module1.js:
const pquire = ; // Smartconst util = ; // <root>/lib/util.jsconst dup1 = ; // <root>/src/duplicate.js// Explicitconst mod2 = pquirerel"mod2"; // <root>/src/mod2.jsconst dup2 = pquire; // <root>/duplicate.js
Advanced usage: global
<root>/index.js:
// ...globalpquire = ;// ...
<root>/lib/util.js:
// No need to require pquire in this file.const settings = ; // <root>/lib/settings.js// ...
Advanced usage: withBaseRelative
<root>
┗╸src
┣╸util
┃ ┗╸settings.js
┣╸sub
┃ ┗╸module1.js
┣╸mod2.js
┗╸index.js
<root>/src/index.js:
// ...// Set the base path to '<root>/src'globalpquire = ;// ...
<root>/src/sub/module1.js:
// No need to require pquire in this file.const settings = ; // <root>/src/util/settings.js// ...