/ archiveofbelonging.org / back / node_modules / optional-require /

[ICO]NameLast modifiedSizeDescription
[PARENTDIR]Parent Directory  -  
[TXT]README.md40 years ago3.7Kf12eb36 documentaiton updates [كارل مبارك]
[   ]index.js40 years ago2.8K 
[   ]package.json2 years ago1.8K7375cab EXHIBTION: fix overflow ellipsis cutoff [كارل مبارك]
README.md

NPM version Build Status Dependency Status devDependency Status

Optional Require

NodeJS Require that let you handle module not found error without try/catch. Allows you to gracefully require a module only if it exists and contains no error.

Usage

const optionalRequire = require("optional-require")(require);

const foo = optionalRequire("foo") || {};
const bar = optionalRequire("bar", true); // true enables console.log a message when not found
const xyz = optionalRequire("xyz", "test"); // "test" enables console.log a message with "test" added.
const fbPath = optionalRequire.resolve("foo", "foo doesn't exist");
const rel = optionalRequire("../foo/bar"); // relative module path works

Install

$ npm i optional-require --save

API

optionalRequire(require)

The single function this module exports. Call it with require to get a custom function for you to do optional require from your file's require context. See Usage above.

customOptionalRequire(path, [message|options])

The function optionalRequire returns for you to do optional require from your file's require context.

Params
Returns
Throws

customOptionalRequire.resolve(path, [message])

Same as customOptionalRequire but acts like require.resolve

optionalRequire.log(message, path)

The function that will be called to log the message when optional module is not found. You can override this with your own function.

optionalRequire.try(require, path, [message|options])

Same as customOptionalRequire but you have to pass in require from your file's context.

optionalRequire.resolve(require, path, [message|options])

Same as customOptionalRequire.resolve but you have to pass in require from your file's context.

LICENSE

Apache-2.0 © Joel Chen

Apache/2.4.38 (Debian) Server at www.karls.computer Port 80