eslint-scope
raw JSON → 9.1.2 verified Sat Apr 25 auth: no javascript
ESLint Scope is the ECMAScript scope analyzer used in ESLint, forked from escope. The current stable version is 9.1.2, with regular releases as part of the ESLint monorepo. It analyzes ESTree-compliant ASTs to produce a ScopeManager with variable scopes, references, and definitions. Key differentiators include deep integration with ESLint, support for JSX, modern ECMAScript features, and both ESM and CommonJS module analysis. It ships with TypeScript types since v9.1.0 and requires Node.js ^20.19.0 || ^22.13.0 || >=24.
Common errors
error TypeError: eslintScope.analyze is not a function ↓
cause Using default import instead of namespace import in ESM
fix
Replace 'import eslintScope from "eslint-scope"' with 'import * as eslintScope from "eslint-scope"'.
error Cannot find module 'eslint-scope' ↓
cause Package not installed or wrong import path when using CommonJS with .mjs extension
fix
Ensure 'eslint-scope' is listed in package.json dependencies and run npm install. For .mjs files, use ESM import syntax.
error TypeError: scopeManager.acquire is not a function ↓
cause Calling acquire on a Scope object instead of ScopeManager
fix
Use scopeManager.acquire(node), not scope.acquire(node).
error Error: The 'sourceType' option must be "script" or "module" ↓
cause Providing an invalid sourceType value
fix
Set sourceType to either "script" or "module" (e.g., sourceType: "module").
Warnings
breaking Requires Node.js ^20.19.0 || ^22.13.0 || >=24 as of v9.0.0 ↓
fix Update Node.js to a supported version (20.19.0+, 22.13.0+, or 24+).
breaking Default export removed; use namespace import (ESM) or require (CJS) ↓
fix Change 'import eslintScope from "eslint-scope"' to 'import * as eslintScope from "eslint-scope"'.
gotcha ScopeManager.acquire() may return null for nodes that do not introduce a scope ↓
fix Always check that the return value is not null before using it.
deprecated The 'optimistic' option is experimental and may be removed in future versions ↓
fix Avoid relying on optimistic scope analysis; use default behavior.
gotcha ECMAScript version must match the parser's ecmaVersion; mismatches may cause incorrect scope analysis ↓
fix Provide the same ecmaVersion to both the parser and eslint-scope.analyze().
Install
npm install eslint-scope yarn add eslint-scope pnpm add eslint-scope Imports
- eslintScope wrong
import eslintScope from 'eslint-scope'correctimport * as eslintScope from 'eslint-scope' - eslintScope wrong
const { analyze } = require('eslint-scope')correctconst eslintScope = require('eslint-scope') - ScopeManager wrong
import { ScopeManager } from 'eslint-scope'correctimport type { ScopeManager } from 'eslint-scope' - Scope wrong
const { Scope } = require('eslint-scope')correctimport type { Scope } from 'eslint-scope'
Quickstart
import * as eslintScope from 'eslint-scope';
import * as espree from 'espree';
const code = `const x = 1; function foo() { var y = 2; }`;
const ast = espree.parse(code, { ecmaVersion: 2022, sourceType: 'script' });
const scopeManager = eslintScope.analyze(ast, { ecmaVersion: 2022 });
console.log(scopeManager.scopes.length); // 2
console.log(scopeManager.globalScope.variables.map(v => v.name)); // ['x', 'foo']
const functionScope = scopeManager.acquire(ast.body[1]);
console.log(functionScope.variables.map(v => v.name)); // ['y']