{"id":20134,"library":"imports-visitor","title":"imports-visitor","description":"Babel visitor that simplifies working with import and require calls in AST traversal. Version 2.1.0 provides ImportDefinition objects for each imported symbol, supporting both ESM imports and CJS require, dynamic imports, type imports, and re-exports. Designed for use in babel plugins or with @babel/traverse, it offers methods like remove() and fork() to modify import statements. Differentiates from higher-level tools like transform-imports by giving direct access to the underlying AST nodes with a minimal, focused API. Actively maintained with peer dependencies on @babel/core, @babel/runtime, and @babel/types (compatible with Babel 7+).","status":"active","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/suchipi/transform-imports","tags":["javascript","transform","codemod","rewrite","import","require","recast","babel","jscodeshift"],"install":[{"cmd":"npm install imports-visitor","lang":"bash","label":"npm"},{"cmd":"yarn add imports-visitor","lang":"bash","label":"yarn"},{"cmd":"pnpm add imports-visitor","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency: required to use the visitor in a Babel plugin or with @babel/traverse.","package":"@babel/core","optional":false},{"reason":"Peer dependency: required at runtime for babel helper functions.","package":"@babel/runtime","optional":false},{"reason":"Peer dependency: used for type definitions and AST node creation.","package":"@babel/types","optional":false}],"imports":[{"note":"Default export; named destructure will fail. Use default import or require with .default.","wrong":"const { importsVisitor } = require('imports-visitor')","symbol":"importsVisitor","correct":"import importsVisitor from 'imports-visitor'"},{"note":"ImportDefinition is a TypeScript type only; use type import to avoid runtime errors.","wrong":"import { ImportDefinition } from 'imports-visitor'","symbol":"ImportDefinition","correct":"import type { ImportDefinition } from 'imports-visitor'"},{"note":"VisitorState is a TypeScript type for the state object passed to the visitor.","wrong":"import { VisitorState } from 'imports-visitor'","symbol":"VisitorState","correct":"import type { VisitorState } from 'imports-visitor'"}],"quickstart":{"code":"const importsVisitor = require('imports-visitor');\nconst babel = require('@babel/core');\n\nconst code = `\nimport React from 'react';\nimport { useState } from 'react';\nconst _ = require('lodash');\n`;\n\nconst plugin = () => ({\n  visitor: {\n    Program(path) {\n      const imports = [];\n      path.traverse(importsVisitor, { imports });\n      console.log('Found imports:', imports.length);\n      imports.forEach(i => {\n        console.log(`  var: ${i.variableName}, source: ${i.source}, kind: ${i.kind}`);\n      });\n    },\n  },\n});\n\nconst result = babel.transformSync(code, { plugins: [plugin] });\n// Output:\n// Found imports: 3\n//   var: React, source: react, kind: value\n//   var: useState, source: react, kind: value\n//   var: _, source: lodash, kind: value","lang":"javascript","description":"Demonstrates using importsVisitor in a Babel plugin to collect ImportDefinition objects and log them."},"warnings":[{"fix":"Use require('imports-visitor').default or import importsVisitor from 'imports-visitor' instead of destructuring.","message":"Default import with require yields undefined if destructured incorrectly.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use import type { ImportDefinition } from 'imports-visitor' for TypeScript, or omit completely in plain JS.","message":"ImportDefinition is a TypeScript type only; attempting to import it as a value will cause a runtime error.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Always pass a fresh array (e.g., { imports: [] }) on each traverse call.","message":"The visitor mutates the state object passed in. If you traverse the same path multiple times without resetting state, imports array accumulates duplicates.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use fork({ insert: 'after' }) for the common case. Check documentation for full options.","message":"Deprecated: The fork() method without arguments no longer inserts after; you must explicitly pass { insert: 'before' | 'after' }.","severity":"deprecated","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use: const importsVisitor = require('imports-visitor').default; OR import importsVisitor from 'imports-visitor';","cause":"Using named import when package exports a default function.","error":"TypeError: importsVisitor is not a function"},{"fix":"Run: npm install --save-dev @babel/core @babel/runtime @babel/types","cause":"Missing peer dependency @babel/core.","error":"Cannot find module '@babel/core'"},{"fix":"Change to: import type { ImportDefinition } from 'imports-visitor';","cause":"Using a regular import instead of a type-only import for ImportDefinition.","error":"TypeScript error: 'ImportDefinition' refers to a type, but is being used as a value here."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}