babel-plugin-import-glob
raw JSON →Babel plugin that enables importing modules using glob patterns, allowing developers to import multiple modules from a directory in a single statement. Current stable version is 2.0.0, released with breaking changes: the glob: prefix is now optional, patterns must be relative (starting with ./ or ../), and identifiers are generated from the dynamic portions of the pattern. This plugin is tested with Node.js 4+. It supports default member import, namespace import, side-effect import, and member aliasing, but does not support importing the default export. The underlying mechanism uses the 'glob' package for pattern matching and 'identifierfy' for converting match strings to valid JavaScript identifiers. Key differentiators: avoids manual per-module imports, useful for template directories or side-effect modules, but not recommended for general use due to potential maintainability issues.
Common errors
error SyntaxError: import from 'pattern' must start with './' or '../' ↓
error SyntaxError: ... is not a valid identifier ↓
error SyntaxError: Cannot import default from a glob pattern ↓
error SyntaxError: No matches found for pattern ↓
Warnings
breaking Patterns must be relative (start with ./ or ../) since v2.0.0. Absolute paths or bare names will throw SyntaxError. ↓
breaking Identifiers are now derived from dynamic portions of the pattern only, not from common path segments or extensions. v1.x behavior differs. ↓
gotcha Cannot import default export from glob pattern. Attempting to do so throws SyntaxError. ↓
gotcha Importing a member name that does not correspond to any matched file throws SyntaxError. ↓
gotcha Identifier collision from multiple matches (e.g., two files generating same name) throws SyntaxError. ↓
gotcha The glob: prefix is optional in v2.0.0, but if used with wrong casing (e.g., Glob:) causes issues. ↓
deprecated Node.js <4 is not supported as of v2.0.0. Plugin may fail on older runtimes. ↓
Install
npm install babel-plugin-import-glob yarn add babel-plugin-import-glob pnpm add babel-plugin-import-glob Imports
- default wrong
import myModule from './path/**/*.js'correctNot supported; use named imports or namespace import. - named wrong
import { doesNotExist } from './templates/**/*.handlebars.js'correctimport { main } from './templates/**/*.handlebars.js' - namespace wrong
import * as templates from 'glob:./templates/**/*.handlebars.js' (prefix is optional)correctimport * as templates from './templates/**/*.handlebars.js'
Quickstart
// .babelrc
{
"plugins": ["import-glob"]
}
// index.js
// Assuming files: templates/main.handlebars.js and templates/_partial.handlebars.js
import { main, _partial as partial } from './templates/**/*.handlebars.js';
import * as all from './templates/**/*.handlebars.js';
import './side-effects/*.js'; // Side-effect import
console.log(main); // exports from templates/main.handlebars.js
console.log(partial); // aliased from _partial
console.log(all.main); // via namespace