{"id":10476,"library":"acorn-globals","title":"Detect Global Variables in JavaScript ASTs","description":"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.","status":"active","version":"7.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/ForbesLindesay/acorn-globals","tags":["javascript","ast","variable","name","lexical","scope","local","global","implicit"],"install":[{"cmd":"npm install acorn-globals","lang":"bash","label":"npm"},{"cmd":"yarn add acorn-globals","lang":"bash","label":"yarn"},{"cmd":"pnpm add acorn-globals","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core parsing library for generating the AST. Version 8 is required since acorn-globals v7.0.0.","package":"acorn","optional":false}],"imports":[{"note":"The primary function `detect` is a default export.","wrong":"import { detect } from 'acorn-globals';","symbol":"detect","correct":"import detect from 'acorn-globals';"},{"note":"CommonJS require style, still supported for Node.js environments.","symbol":"detect","correct":"const detect = require('acorn-globals');"}],"quickstart":{"code":"import fs from 'node:fs';\nimport path from 'node:path';\nimport detect from 'acorn-globals';\n\n// Create a dummy input file for demonstration\nconst inputJsPath = path.join(process.cwd(), 'input.js');\nconst srcContent = `\nvar x = 5;\nvar y = 3, z = 2;\n\nw.foo();\nw = 2;\n\nRAWR=444;\nRAWR.foo();\n\nBLARG=3;\n\nfoo(function () {\n    var BAR = 3;\n    process.nextTick(function (ZZZZZZZZZZZZ) {\n        console.log('beep boop');\n        var xyz = 4;\n        x += 10;\n        x.zzzzzz;\n        ZZZ=6;\n    });\n    function doom () {\n    }\n    ZZZ.foo();\n\n});\n\nconsole.log(xyz);\n`;\n\nfs.writeFileSync(inputJsPath, srcContent, 'utf8');\n\n// Read the source code\nconst src = fs.readFileSync(inputJsPath, 'utf8');\n\n// Detect global variables\nconst scope = detect(src);\n\nconsole.log('Detected globals:');\nscope.forEach(globalVar => {\n  console.log(`- ${globalVar.name} (found at positions: ${globalVar.nodes.map(node => node.start).join(', ')})`);\n});\n\n// Clean up the dummy file\nfs.unlinkSync(inputJsPath);\n","lang":"javascript","description":"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."},"warnings":[{"fix":"Update your `acorn` dependency to version 8 or later: `npm install acorn@^8`.","message":"`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.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"If specific parsing options are needed, explicitly pass them to the `detect` function via the `acorn` option: `detect(src, { acorn: { ecmaVersion: 2015 } })`.","message":"Starting 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.","severity":"breaking","affected_versions":">=7.0.0"},{"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.","message":"Version 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.","severity":"breaking","affected_versions":">=6.0.0 <7.0.0"},{"fix":"Upgrade to `acorn-globals@7.0.1` or newer to correctly handle block-scoped variables within switch statements.","message":"Prior 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`.","severity":"gotcha","affected_versions":">=6.0.0 <7.0.1"},{"fix":"Upgrade to `acorn-globals@4.3.2` or newer to ensure correct block-scoping for classes.","message":"Older 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.","severity":"gotcha","affected_versions":"<4.3.2"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use `import detect from 'acorn-globals';` for ESM, or `const detect = require('acorn-globals');` for CommonJS.","cause":"Attempting to destructure the default export or using `require` incorrectly in an ESM module.","error":"TypeError: detect is not a function"},{"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' } })`.","cause":"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`).","error":"SyntaxError: Unexpected token (XX:YY)"},{"fix":"Install a compatible version of `acorn`. For `acorn-globals` v7+, use `npm install acorn@^8`.","cause":"The `acorn` package is a peer dependency but is not installed or the installed version is incompatible with `acorn-globals`.","error":"Error: Cannot find module 'acorn'"}],"ecosystem":"npm"}