Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
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.TypeScript:
import { optionalRequire } from "optional-require";
const some = optionalRequire("some-optional-module");
JavaScript:
const { optionalRequire } = require("optional-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");
// relative module path works - *but* you need to pass in `require` from your file
const rel = optionalRequire("../foo/bar", { require });
require
The default optionalRequire
uses require
from the context of this module. While you can pass in your require
in options
, if you want to create your own function that's bound to your require
, you can do it with makeOptionalRequire
:
import { makeOptionalRequire } from "optional-require";
const optionalRequire = makeOptionalRequire(require);
// now you can optional require files in same dir as your file
const myModule = optionalRequire("./my-module");
In older versions, this module exports makeOptionalRequire
directly and this is the legacy usage in JavaScript, 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
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
We found that optional-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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.