ast-walker-scope

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

Traverse Babel AST with scope information. v0.9.0 is the current stable version, released with breaking changes dropping the types field and fixing a prototype pollution vulnerability. The package is ESM-only from v0.8.0, requires Node >=20.19.0, and builds on top of estree-walker. Key differentiators: scope tracking per node, support for Babel AST, and a simple hook context with scope, scopes, and level properties. Compared to estree-walker, it adds scope-aware traversal without a separate scope analysis step.

error ERR_REQUIRE_ESM: require() of ES Module /path/to/node_modules/ast-walker-scope/dist/index.js from /path/to/project/index.js not supported.
cause Package is ESM-only from v0.8.0 (v0.6.0 if using default ESM).
fix
Replace require('ast-walker-scope') with import { walk } from 'ast-walker-scope' or use dynamic import().
error TypeError: walk is not a function
cause Incorrect import pattern (e.g., default import instead of named import).
fix
Use import { walk } from 'ast-walker-scope' (named import).
error Module not found: Can't resolve 'ast-walker-scope' in '/path/to/project'
cause Package not installed or misspelled.
fix
Run npm install ast-walker-scope and ensure the import path is correct.
breaking Removed types field from package.json; TypeScript types may not be resolvable without separate types export.
fix Use `import type` and ensure your tsconfig includes `node_modules/ast-walker-scope` types correctly; or install `@types/ast-walker-scope` if available.
breaking Dropped CommonJS build and Node.js 18 support.
fix Migrate to ESM syntax (`import` instead of `require`). Upgrade Node.js to >=20.19.0.
breaking Dropped Node.js 16 support.
fix Upgrade Node.js to >=18 or later (but note >=0.8.0 requires >=20.19.0).
breaking Default to ESM from v0.6.0; CJS require() no longer works without dynamic import.
fix Switch to `import` syntax or use dynamic `import('ast-walker-scope')`.
deprecated Prototype pollution via __proto__ identifier was fixed in v0.9.0.
fix Upgrade to v0.9.0 or later to avoid potential prototype pollution.
npm install ast-walker-scope
yarn add ast-walker-scope
pnpm add ast-walker-scope

Demonstrates basic usage of walk() with scope tracking in enter/leave hooks.

import { walk } from 'ast-walker-scope';

const code = `
const a = 1;
{
  const b = 2;
  console.log(a, b);
}
`.trim();

walk(code, {
  enter(node) {
    console.log(`Enter: ${node.type}`);
  },
  leave(node) {
    if (node.type === 'CallExpression') {
      console.log('Scope variables:', Object.keys(this.scope));
    }
  },
});