Registry / devops / babel-helper-module-imports

babel-helper-module-imports

JSON →
library7.0.0-beta.3jsnpmunverified

Babel helper to insert module imports programmatically in AST transforms. v7.0.0-beta.3 is a prerelease of the 7.x series, part of the Babel ecosystem. This package provides functions like addDefault, addNamed, addSideEffect, and addNamespace to inject import statements during plugin execution. It abstracts away import creation, handling scoping, name collisions, and path insertion. The stable 7.x line is in maintenance; 8.x is in development with breaking changes. Key differentiator: designed for Babel plugin authors needing to add imports without manual AST construction.

npm install babel-helper-module-imports
INSTALL
IMPORT
SIG · BABEL-HELPER-MODUL
B
babel-helper-module-imports
devopsjavascriptv7.0.0-beta.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

addDefault
import { addDefault } from 'babel-helper-module-imports'
const addDefault = require('babel-helper-module-imports').addDefault
ESM syntax is preferred; CJS require also works in Node.js.
addNamed
import { addNamed } from 'babel-helper-module-imports'
import addNamed from 'babel-helper-module-imports'
addNamed is a named export, not a default export.
addSideEffect
import { addSideEffect } from 'babel-helper-module-imports'
Used for side-effect-only imports (e.g., 'import "source"').
addNamespace
import { addNamespace } from 'babel-helper-module-imports'
Creates namespace imports like import * as X from 'source'.

Demonstrates adding default and named imports in a Babel plugin using helper functions.

import { addDefault, addNamed } from 'babel-helper-module-imports'; import { types as t } from '@babel/core'; export default function() { return { visitor: { Program(path) { // Add default import: import lodash from 'lodash' const defaultImport = addDefault(path, 'lodash'); // Add named import: import { map } from 'lodash' const namedImport = addNamed(path, 'map', 'lodash', { nameHint: 'map' }); // Use the imported identifiers console.log(defaultImport.name); // e.g., '_lodash' console.log(namedImport.name); // e.g., '_map' } } }; }
Debug
Known issues
breakingAPI changes across major versions: Babel 8 may alter function signatures or import paths.
fix
Refer to Babel 8 migration guide; test plugins against target version.
affects: >=8.0.0
deprecatedThe package name 'babel-helper-module-imports' is legacy; Babel 7+ uses scoped packages under @babel/.
fix
Prefer @babel/helper-module-imports in modern projects.
affects: >=7.0.0
breakingRemoval of default export in Babel 8: Some packages may drop default export support.
fix
Use named imports only.
affects: >=8.0.0-rc.2
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'type')
addDefault or addNamed called with invalid path (e.g., not a Program or statement path).
fix
Ensure the first argument is a valid Babel path (e.g., path from visitor).
Upgrade
Version history
7.0.0-beta.3latest on npm
Audit
Dependencies
@babel/typesrequiredUsed for creating AST nodes for import declarations
Agent activity
16 hits · last 30 days
node
12
OpenAI (training)
2
Resources
babel-helper-module-imports — npm install babel-helper-module-imports · libregistry