acorn-globals is a utility for identifying global variable references within JavaScript code by leveraging the Acorn AST parser. It traverses the Abstract Syntax Tree (AST) generated by Acorn to distinguish between locally declared variables and those that implicitly refer to the global scope. The package is currently at version 7.0.1 and appears to maintain an active release cadence, primarily driven by updates to its underlying Acorn dependency and bug fixes related to scope resolution (e.g., switch statement bodies, catch handlers). Its key differentiator is its focus solely on global variable detection based on AST analysis, providing precise lexical scope information without performing any runtime evaluation. It is particularly useful for static analysis tools, linters, and bundlers that need to understand variable leakage or undeclared globals.
npm install acorn-globalsVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use `acorn-globals` to parse a JavaScript string and identify all implicit global variable references, logging their names and their respective AST node positions.
Update your `acorn` dependency to version 8 or later: `npm install acorn@^8`.
If specific parsing options are needed, explicitly pass them to the `detect` function via the `acorn` option: `detect(src, { acorn: { ecmaVersion: 2015 } })`.Ensure your `acorn` dependency is compatible with v7 if upgrading to `acorn-globals@6`. Consider upgrading directly to `acorn-globals@7` and `acorn@8` for the latest features and fixes.
Upgrade to `acorn-globals@7.0.1` or newer to correctly handle block-scoped variables within switch statements.
Upgrade to `acorn-globals@4.3.2` or newer to ensure correct block-scoping for classes.
Use `import detect from 'acorn-globals';` for ESM, or `const detect = require('acorn-globals');` for CommonJS.Upgrade `acorn-globals` to v7+ (which defaults to `ecmaVersion: 'latest'`) or explicitly pass `ecmaVersion: 'latest'` or a higher year (e.g., `ecmaVersion: 2020`) in the `acorn` options: `detect(src, { acorn: { ecmaVersion: 'latest' } })`.Install a compatible version of `acorn`. For `acorn-globals` v7+, use `npm install acorn@^8`.