Registry / serialization / imports-visitor

imports-visitor

JSON →
library2.1.0jsnpmunverified

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+).

npm install imports-visitor
INSTALL
IMPORT
SIG · IMPORTS-VISITOR
I
imports-visitor
serializationjavascriptv2.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

importsVisitor
import importsVisitor from 'imports-visitor'
const { importsVisitor } = require('imports-visitor')
Default export; named destructure will fail. Use default import or require with .default.
ImportDefinition
import type { ImportDefinition } from 'imports-visitor'
import { ImportDefinition } from 'imports-visitor'
ImportDefinition is a TypeScript type only; use type import to avoid runtime errors.
VisitorState
import type { VisitorState } from 'imports-visitor'
import { VisitorState } from 'imports-visitor'
VisitorState is a TypeScript type for the state object passed to the visitor.

Demonstrates using importsVisitor in a Babel plugin to collect ImportDefinition objects and log them.

const importsVisitor = require('imports-visitor'); const babel = require('@babel/core'); const code = ` import React from 'react'; import { useState } from 'react'; const _ = require('lodash'); `; const plugin = () => ({ visitor: { Program(path) { const imports = []; path.traverse(importsVisitor, { imports }); console.log('Found imports:', imports.length); imports.forEach(i => { console.log(` var: ${i.variableName}, source: ${i.source}, kind: ${i.kind}`); }); }, }, }); const result = babel.transformSync(code, { plugins: [plugin] }); // Output: // Found imports: 3 // var: React, source: react, kind: value // var: useState, source: react, kind: value // var: _, source: lodash, kind: value
Debug
Known issues
gotchaDefault import with require yields undefined if destructured incorrectly.
fix
Use require('imports-visitor').default or import importsVisitor from 'imports-visitor' instead of destructuring.
affects: >=1.0.0
gotchaImportDefinition is a TypeScript type only; attempting to import it as a value will cause a runtime error.
fix
Use import type { ImportDefinition } from 'imports-visitor' for TypeScript, or omit completely in plain JS.
affects: >=2.0.0
gotchaThe visitor mutates the state object passed in. If you traverse the same path multiple times without resetting state, imports array accumulates duplicates.
fix
Always pass a fresh array (e.g., { imports: [] }) on each traverse call.
affects: >=1.0.0
deprecatedDeprecated: The fork() method without arguments no longer inserts after; you must explicitly pass { insert: 'before' | 'after' }.
fix
Use fork({ insert: 'after' }) for the common case. Check documentation for full options.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: importsVisitor is not a function
Using named import when package exports a default function.
fix
Use: const importsVisitor = require('imports-visitor').default; OR import importsVisitor from 'imports-visitor';
Cannot find module '@babel/core'
Missing peer dependency @babel/core.
fix
Run: npm install --save-dev @babel/core @babel/runtime @babel/types
TypeScript error: 'ImportDefinition' refers to a type, but is being used as a value here.
Using a regular import instead of a type-only import for ImportDefinition.
fix
Change to: import type { ImportDefinition } from 'imports-visitor';
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
@babel/corerequiredPeer dependency: required to use the visitor in a Babel plugin or with @babel/traverse.
@babel/runtimerequiredPeer dependency: required at runtime for babel helper functions.
@babel/typesrequiredPeer dependency: used for type definitions and AST node creation.
Agent activity
14 hits · last 30 days
node
12
Resources
imports-visitor — npm install imports-visitor · libregistry