https://github.com/thislooksfun/pquire
https://github.com/thislooksfun/pquire
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/thislooksfun/pquire
- Owner: thislooksfun
- License: gpl-3.0
- Created: 2018-03-20T18:29:12.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-30T18:52:34.000Z (over 3 years ago)
- Last Synced: 2025-09-17T09:38:21.225Z (10 months ago)
- Language: JavaScript
- Size: 369 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
pquire
======
[](https://www.npmjs.com/package/pquire)
[](https://www.npmjs.com/package/pquire)
[](https://www.npmjs.com/package/pquire)
[](https://github.com/thislooksfun/pquire/blob/master/LICENSE)
[](https://travis-ci.org/thislooksfun/pquire)
[](https://coveralls.io/github/thislooksfun/pquire?branch=master)
[](https://david-dm.org/thislooksfun/pquire)
[](https://david-dm.org/thislooksfun/pquire#info=devDependencies)
A simple module for better local requiring
## Installation
```
npm i -S pquire
```
## Typical Usage
File structure:
```
┣╸lib
┃ ┣╸settings.js
┃ ┗╸util.js
┣╸src
┃ ┣╸module1.js
┃ ┣╸duplicate.js
┃ ┗╸mod2.js
┣╸duplicate.js
┗╸index.js
```
## Documentation
| Function | Description |
|----------------------|-------------------------------------------------------------------------------------------------------------------------|
| `pquire()` | First tries to require relative to the current file, then, if that fails, requires relative to the package root. |
| `pquire.abs()` | Forces the require path to be relative to the root of the project. |
| `pquire.rel()` | Forces the require path to be relative to the path of the current file. This is the same as running `require(./)` |
| `pquire.withBaseRelative()` | Creates a new pquire instance that uses `` as its absolute base instead of getting it dynamically. This differs from `withBaseAbsolute` in that it interprets `` relative to the current file. |
| `pquire.withBaseAbsolute()` | Creates a new pquire instance that uses `` as its absolute base instead of getting it dynamically. This differs from `withBaseRelative` in that it interprets `` relative to the project root. |
## Examples
File structure:
```
┣╸lib
┃ ┣╸settings.js
┃ ┗╸util.js
┣╸src
┃ ┣╸module1.js
┃ ┣╸duplicate.js
┃ ┗╸mod2.js
┣╸duplicate.js
┗╸index.js
```
### Typical usage:
`/src/module1.js:`
```javascript
const pquire = require("pquire");
// Smart
const util = pquire("lib/util"); // /lib/util.js
const dup1 = pquire("duplicate"); // /src/duplicate.js
// Explicit
const mod2 = pquire.rel("mod2"); // /src/mod2.js
const dup2 = pquire.abs("duplicate"); // /duplicate.js
```
### Advanced usage: global
`/index.js:`
```javascript
// ...
global.pquire = require("pquire");
// ...
```
`/lib/util.js:`
```javascript
// No need to require pquire in this file.
const settings = pquire("settings"); // /lib/settings.js
// ...
```
### Advanced usage: withBaseRelative
```
┗╸src
┣╸util
┃ ┗╸settings.js
┣╸sub
┃ ┗╸module1.js
┣╸mod2.js
┗╸index.js
```
`/src/index.js:`
```javascript
// ...
// Set the base path to '/src'
global.pquire = require("pquire").withBaseRelative("./");
// ...
```
`/src/sub/module1.js:`
```javascript
// No need to require pquire in this file.
const settings = pquire("util/settings"); // /src/util/settings.js
// ...
```