{"id":19119,"library":"babel-plugin-optional-require","title":"babel-plugin-optional-require","description":"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.","status":"maintenance","version":"0.3.1","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/satya164/babel-plugin-optional-require","tags":["javascript","babel-plugin","babel","module","require","import"],"install":[{"cmd":"npm install babel-plugin-optional-require","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-optional-require","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-optional-require","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is a Babel plugin; it's used via .babelrc or babel.config.js config, not imported in user code.","wrong":"import plugin from 'babel-plugin-optional-require'","symbol":"default","correct":"module.exports = { plugins: ['optional-require'] }"}],"quickstart":{"code":"// .babelrc\n{\n  \"plugins\": [\"optional-require\"]\n}\n\n// In your code\nlet a;\ntry {\n  a = require('optional-module');\n} catch (e) {\n  a = null; // fallback\n}","lang":"javascript","description":"Configures Babel to transform optional require calls into IIFE-throwing for missing modules."},"warnings":[{"fix":"Wrap optional requires in try/catch as shown in README.","message":"Plugin only transforms require() calls inside try/catch blocks; uncaught optional requires are left as-is, causing runtime errors if module missing.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Use 'blocklist' and 'allowlist' if available; otherwise treat blacklist/whitelist as stable.","message":"The 'blacklist' and 'whitelist' options are inconsistently named; consider them deprecated in favor of clearer naming in future versions (none planned).","severity":"deprecated","affected_versions":"<=0.3.1"},{"fix":"Use only string literal arguments for optional requires.","message":"Plugin does not handle dynamic expressions in require() (e.g., require(someVar)). Only static string literals are resolved.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run: yarn add --dev babel-plugin-optional-require","cause":"Plugin not installed as devDependency.","error":"Error: Cannot find module 'babel-plugin-optional-require'"},{"fix":"Upgrade to a compatible plugin version or use @babel/plugin-syntax-optional-require? Note: This plugin is for Babel 6 primarily.","cause":"Using Babel 7 with plugin that expects Babel 6 API.","error":"TypeError: this.getOptions is not a function","affected_versions":"babel-plugin-optional-require@0.3.1 with @babel/core@7"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}