
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
try-require
Advanced tools
try/require mechanism to conditionally load a module using require.
npm install try-require --save
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.
Optionally, check require and resolution exceptions with lastError. Note that lastError will return null if no error has ever been triggered, or if the most recent call to require or resolve was successful.
var tryRequire = require('try-require');
var maybe = tryRequire('notAModule');
console.error( tryRequire.lastError() );
Note that both tryRequire and tryRequire.resolve accept an optional second argument if you want to provide your own version of require.
If you would like to add to this library, please ensure that all existing test cases pass and that all new code has proper test coverage in test/all.test.js.
To run tests, simply execute:
npm test
Also, match styles within the project where language of the file allows. Some core styles to follow are:
MIT
FAQs
Conditional load modules.
The npm package try-require receives a total of 61,297 weekly downloads. As such, try-require popularity was classified as popular.
We found that try-require demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.