babel-plugin-import-glob

raw JSON →
2.0.0 verified Mon Apr 27 auth: no javascript

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.

error SyntaxError: import from 'pattern' must start with './' or '../'
cause Glob pattern does not start with ./ or ../ as required since v2.0.0.
fix
Change pattern to start with ./ or ../, e.g., './modules/**/*.js'
error SyntaxError: ... is not a valid identifier
cause The pattern generated a match string that could not be converted to a valid JavaScript identifier (e.g., starting with digit).
fix
Rename matched files to ensure dynamic portions produce valid identifiers.
error SyntaxError: Cannot import default from a glob pattern
cause Using default import (import X from '...') with a glob pattern, which is not supported.
fix
Use named import (import { ... } from '...') or namespace import (import * as X from '...').
error SyntaxError: No matches found for pattern
cause The glob pattern does not match any files in the filesystem.
fix
Verify the pattern syntax and that expected files exist relative to the importing file.
breaking Patterns must be relative (start with ./ or ../) since v2.0.0. Absolute paths or bare names will throw SyntaxError.
fix Prefix glob patterns with ./ or ../
breaking Identifiers are now derived from dynamic portions of the pattern only, not from common path segments or extensions. v1.x behavior differs.
fix Review import member names generated from patterns; they may have changed from v1.
gotcha Cannot import default export from glob pattern. Attempting to do so throws SyntaxError.
fix Use named import or namespace import instead.
gotcha Importing a member name that does not correspond to any matched file throws SyntaxError.
fix Ensure all imported members exist as filenames generating the expected identifiers.
gotcha Identifier collision from multiple matches (e.g., two files generating same name) throws SyntaxError.
fix Adjust filenames to avoid identical dynamic portions after identifier conversion.
gotcha The glob: prefix is optional in v2.0.0, but if used with wrong casing (e.g., Glob:) causes issues.
fix Use exactly 'glob:' (lowercase) or omit it.
deprecated Node.js <4 is not supported as of v2.0.0. Plugin may fail on older runtimes.
fix Upgrade Node.js to version 4 or later.
npm install babel-plugin-import-glob
yarn add babel-plugin-import-glob
pnpm add babel-plugin-import-glob

Shows Babel plugin configuration and three import styles: named with alias, namespace, and side-effect import using glob patterns.

// .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