Registry / serialization / acorn-globals

acorn-globals

JSON →
library7.0.1jsnpmunverified

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-globals
INSTALL
IMPORT
SIG · ACORN-GLOBALS
A
acorn-globals
serializationjavascriptv7.0.1
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.

detect
import detect from 'acorn-globals';
import { detect } from 'acorn-globals';
The primary function `detect` is a default export.
detect
const detect = require('acorn-globals');
CommonJS require style, still supported for Node.js environments.

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.

import fs from 'node:fs'; import path from 'node:path'; import detect from 'acorn-globals'; // Create a dummy input file for demonstration const inputJsPath = path.join(process.cwd(), 'input.js'); const srcContent = ` var x = 5; var y = 3, z = 2; w.foo(); w = 2; RAWR=444; RAWR.foo(); BLARG=3; foo(function () { var BAR = 3; process.nextTick(function (ZZZZZZZZZZZZ) { console.log('beep boop'); var xyz = 4; x += 10; x.zzzzzz; ZZZ=6; }); function doom () { } ZZZ.foo(); }); console.log(xyz); `; fs.writeFileSync(inputJsPath, srcContent, 'utf8'); // Read the source code const src = fs.readFileSync(inputJsPath, 'utf8'); // Detect global variables const scope = detect(src); console.log('Detected globals:'); scope.forEach(globalVar => { console.log(`- ${globalVar.name} (found at positions: ${globalVar.nodes.map(node => node.start).join(', ')})`); }); // Clean up the dummy file fs.unlinkSync(inputJsPath);
Debug
Known issues
breaking`acorn-globals` v7.0.0 introduces a breaking change by upgrading its underlying `acorn` parser dependency to v8. This requires users to ensure their `acorn` dependency is also updated to v8 or higher.
fix
Update your `acorn` dependency to version 8 or later: `npm install acorn@^8`.
affects: >=7.0.0
breakingStarting with v7.0.0, the default `ecmaVersion` passed to the `acorn` parser is set to `'latest'`, ensuring support for the most recent ECMAScript features. Code that relies on older `ecmaVersion` defaults or expects specific parsing behavior might be affected.
fix
If specific parsing options are needed, explicitly pass them to the `detect` function via the `acorn` option: `detect(src, { acorn: { ecmaVersion: 2015 } })`.
affects: >=7.0.0
breakingVersion 6.0.0 of `acorn-globals` updated `acorn` and `acorn-walk` to v7. This was a significant bump from previous `acorn` v6 versions and could introduce parsing behavior changes or require users to update their `acorn` dependency.
fix
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.
affects: >=6.0.0 <7.0.0
gotchaPrior to v7.0.1, variables declared within a `switch` statement body (not directly in `case` blocks but within the statement's scope) were incorrectly identified as global. For example, `switch (3) { case 3: let a; } a; // this 'a' was global`.
fix
Upgrade to `acorn-globals@7.0.1` or newer to correctly handle block-scoped variables within switch statements.
affects: >=6.0.0 <7.0.1
gotchaOlder versions (pre-4.3.2) might incorrectly handle class declarations, misidentifying them as function/module scoped instead of block-scoped, leading to incorrect global detection.
fix
Upgrade to `acorn-globals@4.3.2` or newer to ensure correct block-scoping for classes.
affects: <4.3.2
Errors
Common errors & fixes
TypeError: detect is not a function
Attempting to destructure the default export or using `require` incorrectly in an ESM module.
fix
Use `import detect from 'acorn-globals';` for ESM, or `const detect = require('acorn-globals');` for CommonJS.
SyntaxError: Unexpected token (XX:YY)
The input JavaScript code uses syntax features not supported by the default `ecmaVersion` of the underlying Acorn parser (e.g., modern syntax in an older `ecmaVersion`).
fix
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' } })`.
Error: Cannot find module 'acorn'
The `acorn` package is a peer dependency but is not installed or the installed version is incompatible with `acorn-globals`.
fix
Install a compatible version of `acorn`. For `acorn-globals` v7+, use `npm install acorn@^8`.
Upgrade
Version history
7.0.1latest on npm
Audit
Dependencies
acornrequiredCore parsing library for generating the AST. Version 8 is required since acorn-globals v7.0.0.
Agent activity
48 hits · last 30 days
node
42
Resources
acorn-globals — npm install acorn-globals · libregistry