Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

try-require

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

try-require

Conditional load modules.

Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
57K
-3.86%
Maintainers
1
Weekly downloads
 
Created
Source

try-require

try/require mechanism to conditionally load a module using require.

Installation

npm install try-require --save

Usage

try-require lets you try to require a module and not fail if the module is not installed. You could do this inline but the try/catch block will prevent V8 from optimizing your entire function. Therefore, making try-require standalone means only this module is not optimizable.

Sometimes you don't need to load the module, just determine if it is available. For this, a resolve function is provided with `try-require.``

// Conditionally require a module
var tryRequire = require('try-require');
var maybe = tryRequire('maybeModule');

// If `maybeModule` is not available, then `maybe` will
// be undefined. If available it is equivalent to:
// var maybe = require('maybeModule');
// Determine if a module is available without loading it into memory
var tryRequire = require('try-require');
var maybePath = tryRequire.resolve('maybeModule');

// If available, maybePath holds the path to the module
// and the module is not loaded. If `maybeModule` is not available,
// then `maybePath` will be undefined.

Note that both tryRequire and tryRequire.resolve accept an optional second argument if you want to provide your own version of require.

License

MIT

FAQs

Package last updated on 19 May 2015

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