{"id":19161,"library":"babel-plugin-rewrite-require","title":"babel-plugin-rewrite-require","description":"A Babel plugin that rewrites ES6 module imports and CommonJS require() calls using a simple alias map. Version 1.14.5 is stable with infrequent releases. Key differentiators: supports optional module blacklisting, non-string literal detection, and missing file handling. Unlike generic alias plugins, it is designed for React Native and browserify compatibility, enabling runtime exceptions for optional dependencies or missing files.","status":"active","version":"1.14.5","language":"javascript","source_language":"en","source_url":"https://github.com/silklabs/silk","tags":["javascript","babel","plugin","babel-plugin","module","alias","require","import","silk"],"install":[{"cmd":"npm install babel-plugin-rewrite-require","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-rewrite-require","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-rewrite-require","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Requires Babel core to function as a plugin.","package":"@babel/core","optional":true}],"imports":[{"note":"CommonJS-style export; used in Babel config files.","symbol":"default","correct":"module.exports = require('babel-plugin-rewrite-require')"},{"note":"Default export only; named import will result in undefined.","wrong":"import { rewrite } from 'babel-plugin-rewrite-require'","symbol":"default","correct":"import rewrite from 'babel-plugin-rewrite-require'"}],"quickstart":{"code":"// .babelrc or babel.config.js\n{\n  \"plugins\": [\n    [\"babel-plugin-rewrite-require\", {\n      \"aliases\": {\n        \"some-module\": \"./some-replacement-module\",\n        \"crypto\": \"crypto-browserify\"\n      },\n      \"throwForNonStringLiteral\": true,\n      \"throwForModules\": [\"optional-dep\"],\n      \"throwForMissingFiles\": [\"/path/to/optional/config.json\"]\n    }]\n  ]\n}\n\n// Input: require('some-module') becomes require('./some-replacement-module')\n// Input: require('crypto') becomes require('crypto-browserify')\n// Input: require('cry' + 'pto') throws a runtime error\n// Input: require('optional-dep') throws a runtime error\n// Input: require('/path/to/optional/config.json') throws a runtime error","lang":"javascript","description":"Configures the plugin with aliases, non-string literal enforcement, and module/file blacklisting."},"warnings":[{"fix":"Use module.exports in Babel config file, or use dynamic import with .mjs config.","message":"The plugin is a CommonJS module; importing it in an ES module context may require dynamic import or Babel config as JS.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Use import pkg from 'babel-plugin-rewrite-require' or const pkg = require('babel-plugin-rewrite-require')","message":"The default export must be used; named exports do not exist.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Use current flat object format for aliases.","message":"Plugin options structure may change in future major versions; aliases is currently flat.","severity":"deprecated","affected_versions":"<2.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"npm install babel-plugin-rewrite-require --save-dev","cause":"Plugin not installed or incorrectly referenced.","error":"Error: Cannot find module 'babel-plugin-rewrite-require'"},{"fix":"Use import rewrite from 'babel-plugin-rewrite-require'","cause":"Using named import instead of default import.","error":"TypeError: (0 , _plugin.default) is not a function"},{"fix":"Ensure plugin is specified as [\"babel-plugin-rewrite-require\", { ... }] in .babelrc","cause":"Options passed incorrectly (e.g., as a string instead of object in array form).","error":"ReferenceError: throwForModules is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}