babel-plugin-import-recursive
raw JSON → 1.1.3 verified Sat Apr 25 auth: no javascript
A Babel plugin (v1.1.3) that transforms wildcard import declarations into explicit import statements by scanning the filesystem at compile time. Supports single (*) and double (**) asterisk patterns, snake_case naming, and custom file extensions. Unlike dynamic require or Webpack require.context, this plugin produces static imports suitable for tree-shaking.
Common errors
error Error: Could not find module './actions' ↓
cause The import path points to a directory, but the plugin is not enabled in Babel configuration.
fix
Add 'import-recursive' to your Babel plugins list.
error SyntaxError: Unexpected token * ↓
cause Using wildcard import pattern without the plugin configured.
fix
Ensure babel-plugin-import-recursive is installed and included in Babel config.
Warnings
gotcha Plugin requires a filesystem scan at build time; does not work with dynamic paths or runtime resolution. ↓
fix Ensure the import path is a relative literal string, not a variable or computed expression.
gotcha File naming conventions matter: hyphens are converted to camelCase (e.g., 'action.a' becomes 'actionA'), and underscores are preserved unless snakeCase option is enabled. ↓
fix Use consistent file naming (no special characters) or adjust option to match your naming convention.
gotcha The plugin does not support default exports from the imported modules; it only imports the module object (import * as ...). ↓
fix If you need default exports, use a different pattern or manually re-export.
gotcha Only files with extensions listed in the 'exts' option (default: ["js", "mjs", "jsx"]) are included. TypeScript files are not included by default. ↓
fix Add 'ts' and 'tsx' to the 'exts' array if you use TypeScript.
Install
npm install babel-plugin-import-recursive yarn add babel-plugin-import-recursive pnpm add babel-plugin-import-recursive Imports
- plugin wrong
module.exports = function(api, options) { ... }correctexport default function plugin(api, options) { ... }
Quickstart
// .babelrc
{
"plugins": [
["import-recursive", { "exts": ["js", "jsx", "ts", "tsx"], "snakeCase": false, "nostrip": false }]
]
}
// src/index.js
import actions from './actions/**';
// Generated output:
import * as _actionA from "./actions/action.a";
import * as _actionB from "./actions/action_b";
import * as _actionC from "./actions/sub_dir/actionC";
const _dirImport = {};
_dirImport.actionA = _actionA;
_dirImport.actionB = _actionB;
_dirImport.actionC = _actionC;
const actions = _dirImport;