You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

pquire

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pquire

Better requiring within a package

1.2.0
latest
Source
npm
Version published
Maintainers
1
Created
Source

pquire

npm version npm total downloads npm monthly downloads License
Build status Coveralls
Dependency status DevDependency status

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

FunctionDescription
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 = require("pquire");

// Smart
const util = pquire("lib/util");      // <root>/lib/util.js
const dup1 = pquire("duplicate");     // <root>/src/duplicate.js
// Explicit
const mod2 = pquire.rel("mod2");      // <root>/src/mod2.js
const dup2 = pquire.abs("duplicate"); // <root>/duplicate.js

Advanced usage: global

<root>/index.js:

// ...
global.pquire = require("pquire");
// ...

<root>/lib/util.js:

// No need to require pquire in this file.
const settings = pquire("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'
global.pquire = require("pquire").withBaseRelative("./");
// ...

<root>/src/sub/module1.js:

// No need to require pquire in this file.
const settings = pquire("util/settings"); // <root>/src/util/settings.js
// ...

FAQs

Package last updated on 20 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts