After upgrading from ESLint 8 to ESLint 10, why do files starting with `/* eslint-env browser */` fail linting, and what replaces the comment?
answer
- a comment from the old format
- flat config never honoured it
- an error since 10.0.0
- globals move into languageOptions
- the globals package, scoped by files
basics
~20 seslint-env comments belonged to the removed eslintrc format, and ESLint 10 reports each one as an error. Remove them and declare the globals in eslint.config.js under languageOptions.globals, scoped with files, usually from the globals package.
solid answer
~40 s`/* eslint-env */` comments set per-file environments in the eslintrc system. Flat config never supported them, and since 10.0.0 ESLint reports each one as an error: `/* eslint-env */ comments are no longer supported.` The problem belongs to no rule, so no rule setting switches it off; the comment has to go. Removing it also removes the browser globals it declared, so `no-undef` from the recommended rules then reports `window` and `document`. The replacement is configuration: an object with `files: ["apps/web/**/*.js"]` and `languageOptions: { globals: globals.browser }`, using the separately installed `globals` package, plus `globals.node` for the Node.js scripts. A per-file `/* global name */` comment also still works. The `.eslintrc.json` itself is not read by ESLint 10.
code
javascript · 18 lines// eslint.config.js
import js from "@eslint/js";
import globals from "globals";
import { defineConfig } from "eslint/config";
export default defineConfig([
{ files: ["**/*.js"], extends: [js.configs.recommended] },
{
name: "birdmap/web-globals",
files: ["apps/web/**/*.js"],
languageOptions: { globals: globals.browser },
},
{
name: "birdmap/map-tiles-globals",
files: ["packages/map-tiles/**/*.js"],
languageOptions: { globals: globals.node },
},
]);go deeper
Recall that eslint-env comments no longer work in ESLint 10 and that globals are declared in eslint.config.js instead.
Explain why removing the comments surfaces no-undef reports, and how languageOptions.globals with the globals package and files replaces environments.
Plan the upgrade so the error, the undefined globals and the leftover eslintrc files are handled in order, with globals scoped per runtime rather than repository-wide.
Decide how far to automate the migration and how to review the generated config, so per-runtime globals become an explicit, owned part of the repository.
## Why the comment now fails In ESLint 8, configuration lived in `.eslintrc.*` files, and a file could declare its runtime with an **environment comment** such as `/* eslint-env browser */`. The environment brought in a predefined set of globals (`window`, `document`, ...) for that file. **Flat config** has no environments. ESLint 9 made flat config the default and could still be switched back to eslintrc with `ESLINT_USE_FLAT_CONFIG=false`. ESLint 10.0.0 removed the old format completely, and with it: - `.eslintrc.*` files and the `eslintConfig` key in `package.json` are no longer read. - `/* eslint-env */` comments are **reported as errors** during linting, with the message `/* eslint-env */ comments are no longer supported.` That report has no rule id, so it is not something a `rules` entry can switch off. The documented fix is to remove the comments. ## The second wave: undefined globals Deleting the comments uncovers the real dependency. `no-undef`, which is on in the recommended rules from `@eslint/js`, reports any global the configuration does not declare. A web app that relied on `/* eslint-env browser */` now sees `window` and `document` reported as undefined, and Node.js scripts that relied on `/* eslint-env node */` see `process` reported. The globals must be declared somewhere ESLint 10 reads. ## The replacement: `languageOptions.globals` In flat config, globals live under `languageOptions.globals` in a configuration object, scoped by `files` like any other setting. The `globals` npm package provides the predefined sets; ESLint does not install it for you. ```js // eslint.config.js import js from "@eslint/js"; import globals from "globals"; import { defineConfig } from "eslint/config"; export default defineConfig([ { files: ["**/*.js"], extends: [js.configs.recommended] }, { name: "birdmap/web-globals", files: ["apps/web/**/*.js"], languageOptions: { globals: globals.browser }, }, { name: "birdmap/map-tiles-globals", files: ["packages/map-tiles/**/*.js"], languageOptions: { globals: globals.node }, }, ]); ``` Two alternatives exist for odd cases: - A per-file `/* global describe, it */` comment declares specific globals in one file. - A hand-written object, such as `{ MAP_API_KEY: "readonly" }`, for globals a build tool injects. ## What else the old config carried | eslintrc (ESLint 8) | Flat config (ESLint 10) | |---|---| | `env: { browser: true }` | `languageOptions.globals: globals.browser` | | `/* eslint-env node */` in a file | an object with `files` and `globals.node` | | `globals` at the top level | `languageOptions.globals` | | `overrides: [{ files, rules }]` | a separate configuration object with `files` | | `.eslintignore` | `globalIgnores([...])` | | `extends: ["eslint:recommended"]` | `js.configs.recommended` from `@eslint/js` | | `root: true` | gone; every flat config acts as a root | Pasting eslintrc keys such as `env`, `overrides` or a top-level `globals` into `eslint.config.js` fails validation with a configuration error saying the object appears to be in eslintrc format. `npx @eslint/migrate-config .eslintrc.json` generates a first flat config from the old file; review its output rather than trusting it. ## An upgrade order that works 1. Generate or write `eslint.config.js`, then delete `.eslintrc.json` and `.eslintignore` so nobody edits a file ESLint ignores. 2. Add `globals` objects for each runtime, scoped with `files`. 3. Remove every `eslint-env` comment. 4. Run ESLint and read the remaining `no-undef` reports: each is either a missing global set or a genuine bug. ## Checking the result After the move, confirm that each runtime receives the globals it needs: - `eslint --print-config apps/web/src/map.js` should show browser globals such as `window` under `languageOptions.globals`. - The same command on a file in `packages/map-tiles` should show Node.js globals such as `process`, and no browser ones. - A search of the repository for `eslint-env` should return nothing. - The remaining `no-undef` reports, if any, should each name a real undeclared variable. ## Mistakes worth catching in review - Disabling `no-undef` to silence the second wave, which hides real typos in global names. - Applying `globals.browser` to the whole repository, so Node.js scripts may use `window` unnoticed. - Leaving `.eslintrc.json` in place and editing it, expecting ESLint 10 to read it.
- After removing the `eslint-env` comments, `no-undef` reports `window` across the web app. What is missing?A globals declaration for that code. Add an object with `files: ["apps/web/**/*.js"]` and `languageOptions: { globals: globals.browser }`, importing the `globals` package as a dev dependency. Disabling `no-undef` instead would hide genuine typos in global names.
- Someone pastes `env: { browser: true }` into `eslint.config.js`. What happens?ESLint rejects the configuration with an error saying the object appears to be in eslintrc format, and the run ends without linting. `env` does not exist in flat config; the equivalent is `languageOptions.globals` with a set from the `globals` package.
- Thinks ESLint 10 reads .eslintrc.json when no flat config is found
- Turns no-undef off instead of declaring the missing globals
- Puts env: { browser: true } into an eslint.config.js object
- Applies globals.browser to every file, Node.js scripts included
- Assumes the globals package is installed together with ESLint
- Expects ESLINT_USE_FLAT_CONFIG=false to restore eslintrc in ESLint 10
Assumes ESLint 10: 10.0.0 removed the eslintrc format and reports eslint-env comments as errors; ESLint 9 could still fall back to eslintrc via ESLINT_USE_FLAT_CONFIG=false.