globals

repository·main·Indexed 20 days ago

https://github.com/sindresorhus/globals

A JSON-based mapping of global identifiers for different JavaScript environments, such as browser and Node.js. It provides boolean mappings to indicate if a variable is read-only or may be overwritten, primarily for use by static analysis tools like ESLint to determine available global variables.

Tokens
1.1K
Snippets
6
Records
7
Agent score
20%

What's inside globals

  1. Use globals in your project

    main

    Import the globals package to access environment-specific global variables. Each global identifier is mapped to a boolean value:

    • true: The variable may be overwritten.
    • false: The variable is read-only.

    This boolean mapping is designed for static analysis tools to identify incorrect behavior.

    import globals from 'globals';
    
    console.log(globals.browser);
    /*
    {
    	addEventListener: false,
    	applicationCache: false,
    	ArrayBuffer: false,
    	atob: false,
    	…
    }
    */
  2. Access Node.js globals

    main

    The package provides two distinct sets of globals for Node.js environments:

    1. globals.nodeBuiltin: Globals available to all code running in Node.js (typically properties on globalThis, such as process and Buffer). It does not include CommonJS arguments like require.
    2. globals.node: A combination of nodeBuiltin plus all CommonJS arguments (the "CommonJS module scope"), such as require, module, and exports.

    Tip: When analyzing code that runs outside of a CommonJS wrapper (like JavaScript modules), use nodeBuiltin to help detect accidental CommonJS references.

    import globals from 'globals';
    
    // For standard Node.js globals (process, Buffer, etc.)
    const builtin = globals.nodeBuiltin;
    
    // For Node.js globals plus CommonJS module scope (require, module, etc.)
    const commonjs = globals.node;
  3. Configure Puppeteer download and cache settings

    main

    The puppeteer.config.cjs file allows you to control how Puppeteer downloads browser binaries and where it caches them. This is particularly useful for managing behavior in Continuous Integration (CI) environments versus local development.

    Key configuration options:

    • skipDownload: A boolean determining whether to skip the automatic download of browser binaries. In this configuration, it is set to false (meaning it will download) when process.env.CI is true, and true (skipping download) otherwise.
    • cacheDirectory: Specifies the directory where downloaded browsers are stored. When process.env.CI is true, it uses .cache/puppeteer/ relative to the current directory.
    • chrome: Configuration object for the Chrome browser:
      • skipDownload: Boolean to skip Chrome download.
      • version: The specific version of Chrome to download (defaults to 'latest').
    • firefox: Configuration object for the Firefox browser:
      • skipDownload: Boolean to skip Firefox download.
      • version: The specific version of Firefox to download (defaults to 'latest').
    module.exports = {
    	skipDownload: !IS_CI,
    	cacheDirectory: IS_CI ? path.join(__dirname, '.cache/puppeteer/') : undefined,
    	chrome: {
    		skipDownload: !IS_CI,
    		version: 'latest',
    	},
    	firefox: {
    		skipDownload: !IS_CI,
    		version: 'latest',
    	},
    };
  4. Import global identifiers from globals

    main

    The globals package exports a collection of global identifier objects (such as browser, node, and es) via a JSON structure. You can require or import the package to access these predefined sets of global variables.

    const globals = require('globals');
    
    // Example: Accessing browser globals
    console.log(globals.browser);