
Research
/Security News
Critical Vulnerability in NestJS Devtools: Localhost RCE via Sandbox Escape
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
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.
node.js 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.
So why not just do:
let some;
try {
some = require("some-optional-module");
} catch {
// do nothing
}
let some
before try/catch"some-optional-module"
contains error itself, above code will silently ignore it, leaving you, and more importantly, your users, puzzling on why it's not working -- the original reason that prompted the creation of this package.ES Modules:
import { optionalRequire } from "optional-require";
const some = optionalRequire("some-optional-module");
const bar = optionalRequire("bar", true); // log message when not found
const xyz = optionalRequire("xyz", "test"); // log with custom message
const fbPath = optionalRequire.resolve("foo", "foo doesn't exist");
CommonJS:
const { optionalRequire } = require("optional-require");
const foo = optionalRequire("foo") || {};
const rel = optionalRequire("../foo/bar", { require }); // relative paths need require
To require modules relative to your file, bind the function to your context:
ESM:
import { makeOptionalRequire } from "optional-require";
const optionalRequire = makeOptionalRequire(import.meta.url);
const myModule = optionalRequire("./my-module");
CommonJS:
const { makeOptionalRequire } = require("optional-require");
const optionalRequire = makeOptionalRequire(__dirname);
// or
const optionalRequire = makeOptionalRequire(require);
const myModule = optionalRequire("./my-module");
In older versions, this module exports makeOptionalRequire
directly and this is the legacy usage in CommonJS only, which is still supported:
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
Note: This legacy pattern only works in CommonJS mode since it relies on the require
function.
https://jchip.github.io/optional-require/modules.html#optionalrequire
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 702,516 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.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.
Product
Socket now supports Rust and Cargo, offering package search for all users and experimental SBOM generation for enterprise projects.