Course contentsShow
← Web Development
Lesson 1428 of 2,98226. Code Quality ToolsFree lesson

Environments and Global Variables

Setting env options to define available globals like browser, node, es2021, and jest.

Environments and Global Variables

What you'll learn: How to tell ESLint which runtime environment your code will run in so it knows which global variables are legitimate.

The Problem ESLint Faces

When ESLint analyzes your code, it sees variables like window, document, process, or describe. Are these legitimate globals provided by the browser or Node.js? Or did you make a typo? Without context, ESLint can't tell—so it might warn you about "undefined variables" that are actually built-in.

What Are Environment Settings?

Think of ESLint environments like declaring "where this code lives." Just as you'd pack different items for a beach vacation versus a ski trip, your code needs different globals depending on whether it runs in a browser, on a server, or in a test framework.

The env option in your ESLint config tells the linter: "Hey, these global variables are okay here."

Common Environment Options

// .eslintrc.json
{
  "env": {
    "browser": true,    // allows: window, document, localStorage, etc.
    "node": true,       // allows: process, __dirname, require, etc.
    "es2021": true,     // allows: Promise, Map, Set, BigInt, etc.
    "jest": true        // allows: describe, test, expect, etc.
  }
}

When you set "browser": true, ESLint won't complain when you use window.alert(). Set "node": true and process.env becomes acceptable. Set "jest": true and your test functions are recognized.

You can enable multiple environments simultaneously—many projects use both browser and Node (for tooling scripts), plus a testing framework.

Key Takeaway: The env configuration option tells ESLint which runtime environments your code targets, automatically whitelisting the appropriate global variables for each context.