optional-require
Advanced tools
Comparing version
{ | ||
"name": "optional-require", | ||
"version": "1.1.8", | ||
"version": "2.0.0", | ||
"description": "NodeJS Require that let you handle module not found error without try/catch", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"type": "module", | ||
"main": "./index.cjs", | ||
"types": "./index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist-esm/esm/index.js", | ||
"require": "./index.cjs" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "tsc", | ||
"test": "xrun --serial build xarc/test-only", | ||
"coverage": "xrun --serial build xarc/test-cov", | ||
"publish-util-pre": "publish-util-prepublishonly", | ||
"prepublishOnly": "xrun --serial [[build, docs], xarc/check, publish-util-pre]", | ||
"build": "rm -rf dist-* && tsc --build tsconfig.esm.json && tsc --build tsconfig.cjs.json && ts2mjs --cjs --remove-source --skip-ts dist-cjs", | ||
"test": "vitest run", | ||
"test:coverage": "vitest run --coverage", | ||
"docs": "xrun xarc/docs && touch docs/.nojekyll", | ||
@@ -26,8 +31,9 @@ "postpack": "publish-util-postpack" | ||
"files": [ | ||
"dist", | ||
"index.js", | ||
"dist-cjs", | ||
"dist-esm", | ||
"index.cjs", | ||
"index.d.ts" | ||
], | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=20" | ||
}, | ||
@@ -34,0 +40,0 @@ "author": "Joel Chen", |
@@ -21,17 +21,19 @@ [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] | ||
1. You need to keep the variable outside: `let some` before try/catch | ||
2. If `"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. | ||
1. **Variable scoping**: You need to keep the variable outside: `let some` before try/catch | ||
2. **Error differentiation**: You need additional logic to distinguish between "module not found" vs "module has syntax/runtime errors". If `"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. | ||
3. **Code readability**: Nested try/catch blocks make code harder to read and maintain, especially when dealing with multiple optional modules | ||
## Usage | ||
TypeScript: | ||
```ts | ||
**ES Modules:** | ||
```js | ||
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"); | ||
``` | ||
JavaScript: | ||
**CommonJS:** | ||
```js | ||
@@ -41,26 +43,33 @@ 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 }); | ||
const rel = optionalRequire("../foo/bar", { require }); // relative paths need require | ||
``` | ||
### Binding `require` | ||
### Custom Context | ||
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`: | ||
To require modules relative to your file, bind the function to your context: | ||
```ts | ||
**ESM:** | ||
```js | ||
import { makeOptionalRequire } from "optional-require"; | ||
const optionalRequire = makeOptionalRequire(import.meta.url); | ||
const myModule = optionalRequire("./my-module"); | ||
``` | ||
**CommonJS:** | ||
```js | ||
const { makeOptionalRequire } = require("optional-require"); | ||
const optionalRequire = makeOptionalRequire(__dirname); | ||
// or | ||
const optionalRequire = makeOptionalRequire(require); | ||
// now you can optional require files in same dir as your file | ||
const myModule = optionalRequire("./my-module"); | ||
``` | ||
### Legacy Usage | ||
## Requirements | ||
In older versions, this module exports `makeOptionalRequire` directly and this is the legacy usage in JavaScript, which is still supported: | ||
- **Node.js 20+**: Full support for both ESM and CommonJS through conditional exports | ||
## Legacy Usage | ||
In older versions, this module exports `makeOptionalRequire` directly and this is the legacy usage in **CommonJS only**, which is still supported: | ||
```js | ||
@@ -76,2 +85,4 @@ const optionalRequire = require("optional-require")(require); | ||
**Note**: This legacy pattern only works in CommonJS mode since it relies on the `require` function. | ||
## API | ||
@@ -78,0 +89,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
47647
110.23%20
185.71%786
112.43%101
12.22%Yes
NaN