{"id":20988,"library":"babel-plugin-import-glob","title":"babel-plugin-import-glob","description":"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.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/novemberborn/babel-plugin-import-glob","tags":["javascript","babel-plugin"],"install":[{"cmd":"npm install babel-plugin-import-glob","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-import-glob","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-import-glob","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used for resolving glob patterns to file paths during transpilation.","package":"glob","optional":false},{"reason":"Converts file path fragments to valid JavaScript identifiers for import member names.","package":"identifierfy","optional":false}],"imports":[{"note":"This plugin does not support default imports from glob patterns; attempting so throws a SyntaxError.","wrong":"import myModule from './path/**/*.js'","symbol":"default","correct":"Not supported; use named imports or namespace import."},{"note":"Named imports must correspond to actual matched files. Using the glob: prefix is optional since v2.0.0.","wrong":"import { doesNotExist } from './templates/**/*.handlebars.js'","symbol":"named","correct":"import { main } from './templates/**/*.handlebars.js'"},{"note":"Namespace import collects all default exports of matched modules into an object.","wrong":"import * as templates from 'glob:./templates/**/*.handlebars.js' (prefix is optional)","symbol":"namespace","correct":"import * as templates from './templates/**/*.handlebars.js'"}],"quickstart":{"code":"// .babelrc\n{\n  \"plugins\": [\"import-glob\"]\n}\n\n// index.js\n// Assuming files: templates/main.handlebars.js and templates/_partial.handlebars.js\nimport { main, _partial as partial } from './templates/**/*.handlebars.js';\nimport * as all from './templates/**/*.handlebars.js';\nimport './side-effects/*.js'; // Side-effect import\n\nconsole.log(main); // exports from templates/main.handlebars.js\nconsole.log(partial); // aliased from _partial\nconsole.log(all.main); // via namespace\n","lang":"javascript","description":"Shows Babel plugin configuration and three import styles: named with alias, namespace, and side-effect import using glob patterns."},"warnings":[{"fix":"Prefix glob patterns with ./ or ../","message":"Patterns must be relative (start with ./ or ../) since v2.0.0. Absolute paths or bare names will throw SyntaxError.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Review import member names generated from patterns; they may have changed from v1.","message":"Identifiers are now derived from dynamic portions of the pattern only, not from common path segments or extensions. v1.x behavior differs.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Use named import or namespace import instead.","message":"Cannot import default export from glob pattern. Attempting to do so throws SyntaxError.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Ensure all imported members exist as filenames generating the expected identifiers.","message":"Importing a member name that does not correspond to any matched file throws SyntaxError.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Adjust filenames to avoid identical dynamic portions after identifier conversion.","message":"Identifier collision from multiple matches (e.g., two files generating same name) throws SyntaxError.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Use exactly 'glob:' (lowercase) or omit it.","message":"The glob: prefix is optional in v2.0.0, but if used with wrong casing (e.g., Glob:) causes issues.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Upgrade Node.js to version 4 or later.","message":"Node.js <4 is not supported as of v2.0.0. Plugin may fail on older runtimes.","severity":"deprecated","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Change pattern to start with ./ or ../, e.g., './modules/**/*.js'","cause":"Glob pattern does not start with ./ or ../ as required since v2.0.0.","error":"SyntaxError: import from 'pattern' must start with './' or '../'"},{"fix":"Rename matched files to ensure dynamic portions produce valid identifiers.","cause":"The pattern generated a match string that could not be converted to a valid JavaScript identifier (e.g., starting with digit).","error":"SyntaxError: ... is not a valid identifier"},{"fix":"Use named import (import { ... } from '...') or namespace import (import * as X from '...').","cause":"Using default import (import X from '...') with a glob pattern, which is not supported.","error":"SyntaxError: Cannot import default from a glob pattern"},{"fix":"Verify the pattern syntax and that expected files exist relative to the importing file.","cause":"The glob pattern does not match any files in the filesystem.","error":"SyntaxError: No matches found for pattern"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}