babel-identifiers

raw JSON →
2.0.1 verified Sat Apr 25 auth: no javascript

Classifies Babel AST Identifier nodes into types (binding, reference, static) and grammars (javascript, jsx, flow, typescript). Stable v2.0.1 release with ESM/CJS support. Differentiates from general Babel utilities by focusing exclusively on identifier classification, enabling more precise lint rules and transformation logic. Lightweight with no runtime dependencies.

error Cannot find module 'babel-identifiers'
cause Package not installed or missing from dependencies.
fix
Run: npm install babel-identifiers
error TypeError: getIdentifierKind is not a function
cause Importing incorrectly (e.g., default import instead of named).
fix
Use named import: import { getIdentifierKind } from 'babel-identifiers'
error Argument passed to getIdentifierKind is not a NodePath
cause Passing a raw AST node instead of a Babel NodePath.
fix
Ensure you're inside a visitor callback with 'path' argument, or wrap node with NodePath.
gotcha getIdentifierGrammar may return 'javascript' for identifiers inside JSX if plugin is not enabled.
fix Ensure Babel parser is configured with required plugins (jsx, typescript, flow) matching your code.
gotcha Works only with Babel AST NodePath, not raw AST nodes.
fix Always pass a NodePath object, not a plain node.
gotcha Missing type definitions: package does not ship TypeScript declarations.
fix Create custom .d.ts file or use @types/babel__traverse for type compatibility.
npm install babel-identifiers
yarn add babel-identifiers
pnpm add babel-identifiers

Demonstrates classifying identifiers by type and grammar using Babel parser and traverse.

import { isIdentifierLike, getIdentifierKind, getIdentifierGrammar } from 'babel-identifiers';
import { parse } from '@babel/parser';
import traverse from '@babel/traverse';

const code = `let x = 1; x; <jsx/>; ({y}: number);`;
const ast = parse(code, { sourceType: 'module', plugins: ['jsx', 'typescript', 'flow'] });
traverse(ast, {
  Identifier(path) {
    if (isIdentifierLike(path)) {
      const kind = getIdentifierKind(path);
      const grammar = getIdentifierGrammar(path);
      console.log(path.node.name, kind, grammar);
    }
  }
});