JavaScript Global Identifiers Registry
`globals` is a comprehensive registry of global identifiers found across various JavaScript environments, including browsers, Node.js, Web Workers, and specific frameworks like Vue, Svelte, and Astro. Currently stable at version 17.5.0, the package maintains a frequent release cadence, often monthly, to incorporate updated global lists and introduce new environment definitions. Its core utility is a JSON file (`globals.json`) that maps global variable names to a boolean value, indicating whether the variable is considered writable (`true`) or read-only (`false`), aiding static analysis tools like ESLint. This differentiation is a key feature, allowing linters to flag incorrect assignments to built-in read-only globals. While ESLint 8 and earlier implicitly bundled this package, users of ESLint 9 and later are expected to include `globals` as a direct dependency.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'browser')
cause The `globals` object was not correctly imported or required, or an attempt was made to access a non-existent environment key.fixEnsure `import globals from 'globals';` (ESM) or `const globals = require('globals');` (CJS) is present, and verify the environment key (e.g., `browser`, `node`) exists in the `globals` object. Consult `globals.json` for available keys. -
ReferenceError: require is not defined in ES module scope
cause Attempting to use CommonJS `require()` syntax in an ECMAScript module (ESM) context.fixIf your project or file uses ESM (e.g., `"type": "module"` in `package.json` or `.mjs` files), use `import globals from 'globals';`. If you need to use CommonJS in an ESM project, consider dynamic `import()` or refactoring. -
ESLint not recognizing new globals or environments (e.g., `BigInt` or `process`)
cause The `globals` package is either an outdated version, or ESLint's configuration is not correctly set up to use the `globals` package, especially with ESLint 9+.fixUpdate `globals` to the latest version (`npm install globals@latest`). For ESLint 9+, ensure `globals` is a direct `devDependency` and explicitly configured in your `eslint.config.js` or `.eslintrc.*` file under `languageOptions.globals` for the relevant environments (e.g., `globals.node`, `globals.browser`).
Warnings
- breaking In version 17.0.0, the `audioWorklet` environment was split from the `browser` environment. Code that previously relied on `globals.browser` to include `audioWorklet` specific globals will now need to explicitly reference `globals.audioWorklet`.
- breaking For ESLint version 9 and later, the `globals` package is no longer implicitly bundled by ESLint itself. Users must now include `globals` as a direct dependency in their project's `package.json`.
- gotcha The boolean values associated with each global (`true` or `false`) indicate its writability (true for writable, false for read-only) as understood by static analysis tools. Misinterpreting `true` as merely 'exists' rather than 'is writable' can lead to incorrect linter configurations or unexpected behavior when enforcing coding standards.
- gotcha The package receives frequent updates to its global lists across various environments. While beneficial for staying current, this means specific global identifiers might be added, removed, or have their writability status changed between minor versions, which could subtly impact linting rules or code analysis if not accounted for.
Install
-
npm install globals -
yarn add globals -
pnpm add globals
Imports
- globals
import { globals } from 'globals';import globals from 'globals';
- globals
const { globals } = require('globals');const globals = require('globals'); - globals.browser
import { browser } from 'globals';import globals from 'globals'; const browserGlobals = globals.browser;
Quickstart
import globals from 'globals';
// Access all browser-specific globals
console.log('Browser globals example:');
console.log(Object.keys(globals.browser).slice(0, 5));
// Expected output: [ 'addEventListener', 'applicationCache', 'ArrayBuffer', 'atob', 'Audio' ]
// Access all Node.js built-in globals (excluding CommonJS module scope)
console.log('\nNode.js built-in globals example:');
console.log(Object.keys(globals.nodeBuiltin).slice(0, 5));
// Expected output: [ '__dirname', '__filename', 'Buffer', 'clearImmediate', 'clearInterval' ]
// Check if a specific global is read-only or writable (e.g., 'window' in browser)
console.log(`\n'window' is writable in browser: ${globals.browser.window}`);
// Expected output: 'window' is writable in browser: false (or true depending on exact env config)