{"id":19157,"library":"babel-plugin-require-root-rewrite","title":"babel-plugin-require-root-rewrite","description":"A Babel plugin that transforms module path aliases (e.g., '~/' or '@') into relative paths, allowing developers to require files relative to a configured project root. Version 1.0.2, stable but low activity. It offers a pattern-based replacement with optional basePath and overrides for multi-root projects. Unlike webpack resolve aliases or TypeScript path mapping, this works purely at the Babel transpilation step, making it suitable for Node.js CJS/ESM builds.","status":"active","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/louistakepillz/babel-plugin-require-root-rewrite","tags":["javascript","babel","babel-plugin"],"install":[{"cmd":"npm install babel-plugin-require-root-rewrite","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-require-root-rewrite","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-require-root-rewrite","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This is a Babel plugin, not a runtime import. Configure it in .babelrc or babel.config.js.","wrong":"// Attempting to import in code\nconst plugin = require('babel-plugin-require-root-rewrite');","symbol":"plugin","correct":"// .babelrc\n{\n  \"plugins\": [\"babel-plugin-require-root-rewrite\"]\n}"},{"note":"The plugin has a default export; when using CommonJS require, it returns the plugin function directly.","wrong":"const plugin = require('babel-plugin-require-root-rewrite').default;","symbol":"default","correct":"import plugin from 'babel-plugin-require-root-rewrite'; // if using programmatic API"},{"note":"The pattern is treated as a regex; always anchor it (e.g., '^~/') to avoid accidental matches. Available since v1.0.0.","wrong":"// No regex pattern in pattern\n[\"babel-plugin-require-root-rewrite\", { \"pattern\": \"~\" }]","symbol":"rewrite patterns","correct":"// In .babelrc with options\n[\"babel-plugin-require-root-rewrite\", { \"pattern\": \"^~/\", \"basePath\": \"src\" }]"}],"quickstart":{"code":"// .babelrc\n{\n  \"plugins\": [\n    [\"babel-plugin-require-root-rewrite\", {\n      \"pattern\": \"^~/\",\n      \"basePath\": \"src\",\n      \"overrides\": [\"api\", \"server\"]\n    }]\n  ]\n}\n\n// src/containers/Register.js\nconst BaseAuthForm = require('~/components/BaseAuthForm');\nimport BaseAuthForm from '~/components/BaseAuthForm';","lang":"javascript","description":"Configures the plugin via .babelrc to replace '~/' with relative paths from the 'src' basePath, with overrides for 'api' and 'server' directories."},"warnings":[{"fix":"Use a pattern like '^~/' or '^@/' with the caret anchor.","message":"Pattern is a regex string; ensure you escape special characters (e.g., '^~/') to avoid unintended replacements.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Set basePath to an existing directory (commonly 'src' or 'lib').","message":"BasePath must exist relative to the project root; otherwise, relative paths may be incorrect.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"List directories (not file paths) in overrides array.","message":"Overrides only apply if the file path starts with one of the override strings; all other files use basePath.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Check compatibility with your Babel version; consider alternatives like babel-plugin-module-resolver.","message":"The package uses the legacy Babel 6/7 API; may not work with Babel 8 without updates.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Provide a string pattern, e.g., '^~/'","cause":"Pattern option is not a string or is missing.","error":"Error: Plugin .babelrc: [babel-plugin-require-root-rewrite] pattern is not a string"},{"fix":"Validate your .babelrc with a JSON linter.","cause":"Invalid .babelrc JSON, possibly due to missing quotes or trailing commas.","error":"SyntaxError: Unexpected token / in JSON at position 0"},{"fix":"Run 'npm install babel-plugin-require-root-rewrite --save-dev'","cause":"Plugin not installed or not in node_modules.","error":"Cannot find module 'babel-plugin-require-root-rewrite'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}