Registry / devops / babel-plugin-optional-require

babel-plugin-optional-require

JSON →
library0.3.1jsnpmunverified

Babel plugin to transpile optional require() calls for bundlers like Metro that lack native support. Current stable version 0.3.1, last updated in 2019; low release cadence. Differentiator: replaces missing modules with an error-throwing IIFE so try/catch works at runtime, supports whitelist/blacklist for resolving. Not actively maintained but functional for its niche.

npm install babel-plugin-optional-require
INSTALL
IMPORT
SIG · BABEL-PLUGIN-OPTIO
B
babel-plugin-optional-require
devopsjavascriptv0.3.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
module.exports = { plugins: ['optional-require'] }
import plugin from 'babel-plugin-optional-require'
This is a Babel plugin; it's used via .babelrc or babel.config.js config, not imported in user code.

Configures Babel to transform optional require calls into IIFE-throwing for missing modules.

// .babelrc { "plugins": ["optional-require"] } // In your code let a; try { a = require('optional-module'); } catch (e) { a = null; // fallback }
Debug
Known issues
gotchaPlugin only transforms require() calls inside try/catch blocks; uncaught optional requires are left as-is, causing runtime errors if module missing.
fix
Wrap optional requires in try/catch as shown in README.
affects: >=0.1.0
deprecatedThe 'blacklist' and 'whitelist' options are inconsistently named; consider them deprecated in favor of clearer naming in future versions (none planned).
fix
Use 'blocklist' and 'allowlist' if available; otherwise treat blacklist/whitelist as stable.
affects: <=0.3.1
gotchaPlugin does not handle dynamic expressions in require() (e.g., require(someVar)). Only static string literals are resolved.
fix
Use only string literal arguments for optional requires.
affects: >=0.1.0
Errors
Common errors & fixes
Error: Cannot find module 'babel-plugin-optional-require'
Plugin not installed as devDependency.
fix
Run: yarn add --dev babel-plugin-optional-require
TypeError: this.getOptions is not a function
Using Babel 7 with plugin that expects Babel 6 API.
fix
Upgrade to a compatible plugin version or use @babel/plugin-syntax-optional-require? Note: This plugin is for Babel 6 primarily.
Upgrade
Version history
0.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
babel-plugin-optional-require — npm install babel-plugin-optional-require · libregistry