{"id":26771,"library":"babel-helper-module-imports","title":"babel-helper-module-imports","description":"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.","status":"active","version":"7.0.0-beta.3","language":"javascript","source_language":"en","source_url":"https://github.com/babel/babel/tree/master/packages/babel-helper-module-imports","tags":["javascript"],"install":[{"cmd":"npm install babel-helper-module-imports","lang":"bash","label":"npm"},{"cmd":"yarn add babel-helper-module-imports","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-helper-module-imports","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used for creating AST nodes for import declarations","package":"@babel/types","optional":false}],"imports":[{"note":"ESM syntax is preferred; CJS require also works in Node.js.","wrong":"const addDefault = require('babel-helper-module-imports').addDefault","symbol":"addDefault","correct":"import { addDefault } from 'babel-helper-module-imports'"},{"note":"addNamed is a named export, not a default export.","wrong":"import addNamed from 'babel-helper-module-imports'","symbol":"addNamed","correct":"import { addNamed } from 'babel-helper-module-imports'"},{"note":"Used for side-effect-only imports (e.g., 'import \"source\"').","wrong":null,"symbol":"addSideEffect","correct":"import { addSideEffect } from 'babel-helper-module-imports'"},{"note":"Creates namespace imports like import * as X from 'source'.","wrong":null,"symbol":"addNamespace","correct":"import { addNamespace } from 'babel-helper-module-imports'"}],"quickstart":{"code":"import { addDefault, addNamed } from 'babel-helper-module-imports';\nimport { types as t } from '@babel/core';\n\nexport default function() {\n  return {\n    visitor: {\n      Program(path) {\n        // Add default import: import lodash from 'lodash'\n        const defaultImport = addDefault(path, 'lodash');\n\n        // Add named import: import { map } from 'lodash'\n        const namedImport = addNamed(path, 'map', 'lodash', { nameHint: 'map' });\n\n        // Use the imported identifiers\n        console.log(defaultImport.name); // e.g., '_lodash'\n        console.log(namedImport.name);   // e.g., '_map'\n      }\n    }\n  };\n}","lang":"javascript","description":"Demonstrates adding default and named imports in a Babel plugin using helper functions."},"warnings":[{"fix":"Refer to Babel 8 migration guide; test plugins against target version.","message":"API changes across major versions: Babel 8 may alter function signatures or import paths.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Prefer @babel/helper-module-imports in modern projects.","message":"The package name 'babel-helper-module-imports' is legacy; Babel 7+ uses scoped packages under @babel/.","severity":"deprecated","affected_versions":">=7.0.0"},{"fix":"Use named imports only.","message":"Removal of default export in Babel 8: Some packages may drop default export support.","severity":"breaking","affected_versions":">=8.0.0-rc.2"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure the first argument is a valid Babel path (e.g., path from visitor).","cause":"addDefault or addNamed called with invalid path (e.g., not a Program or statement path).","error":"TypeError: Cannot read properties of undefined (reading 'type')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}