
Product
Socket Now Supports pylock.toml Files
Socket now supports pylock.toml, enabling secure, reproducible Python builds with advanced scanning and full alignment with PEP 751's new standard.
optional-require
Advanced tools
NodeJS Require that let you handle module not found error without try/catch
The 'optional-require' npm package allows you to require modules that may or may not exist without throwing an error. This is useful for optional dependencies or plugins that are not always needed.
Basic Optional Require
This feature allows you to require a module that may not be installed. If the module is not available, it returns null instead of throwing an error.
const optionalRequire = require('optional-require')(require);
const myModule = optionalRequire('my-module');
if (myModule) {
console.log('my-module is available');
} else {
console.log('my-module is not available');
}
Optional Require with Default Value
This feature allows you to provide a default value if the module is not available. This can be useful to ensure your code continues to work even if the optional module is missing.
const optionalRequire = require('optional-require')(require);
const myModule = optionalRequire('my-module', { default: {} });
console.log(myModule);
Optional Require with Logging
This feature allows you to log a custom message if the module is not available. This can be useful for debugging or informing the user about missing optional dependencies.
const optionalRequire = require('optional-require')(require);
const myModule = optionalRequire('my-module', { message: 'my-module is not installed' });
if (!myModule) {
console.log('my-module is not installed');
}
The 'require-optional' package provides similar functionality by allowing you to require modules that may not be installed. It also returns null if the module is not available, but it does not provide options for default values or custom logging messages.
The 'try-require' package attempts to require a module and returns undefined if the module is not found. It is similar to 'optional-require' but does not offer as many customization options such as default values or custom messages.
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.
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
$ npm i optional-require --save
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.
The function optionalRequire returns for you to do optional require from your file's require context.
path
- name/path to the module your want to optionally requiremessage
- optional flag/message to enable console.log
a message when module is not foundoptions
- an optional object with the following fields
message
- see abovefail
- callback for when an error that's not MODULE_NOT_FOUND
for path
occurrednotFound
- callback for when path
was not found
default
- default value to returned when not found - not allowed with notFound
togetherundefined
oroptions.notFound
if it's specifiedoptions.default
if it's specifiedMODULE_NOT_FOUND
for the module path
Same as customOptionalRequire but acts like require.resolve
The function that will be called to log the message when optional module is not found. You can override this with your own function.
Same as customOptionalRequire but you have to pass in require
from your file's context.
Same as customOptionalRequire.resolve but you have to pass in require
from your file's context.
Apache-2.0 © Joel Chen
FAQs
NodeJS Require that let you handle module not found error without try/catch
The npm package optional-require receives a total of 616,194 weekly downloads. As such, optional-require popularity was classified as popular.
We found that optional-require demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Product
Socket now supports pylock.toml, enabling secure, reproducible Python builds with advanced scanning and full alignment with PEP 751's new standard.
Security News
Research
Socket uncovered two npm packages that register hidden HTTP endpoints to delete all files on command.
Research
Security News
Malicious Ruby gems typosquat Fastlane plugins to steal Telegram bot tokens, messages, and files, exploiting demand after Vietnam’s Telegram ban.