Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

ESLint indent rule reports tabs versus spaces on every line

ErrorExpected indentation of 1 tab but found 4 spaces indent

Short answer

The indent rule is set to one style and your files use another. Decide which style the project wants, set the rule to match, then run eslint --fix to rewrite the files. The editor can also fight back: VS Code detects indentation per file and overrides your defaults.

  • Applies to ESLint (current), Node.js 22, VS Code
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the indent setting ESLint actually uses, so you know whether the rule wants tabs or a number of spaces.

    grep -rn "indent" eslint.config.* .eslintrc* 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1Every indented line fails, not just a few.The rule asks for tabs and the files use spaces (or the reverse)Fix
    2. 2You fix a file, save, and the editor puts the other indentation back as you type.VS Code detects indentation and re-indents with the wrong styleFix
    3. 3You upgraded ESLint and formatting rules now come from two places.The rule is deprecated and a style plugin takes overFix

What does the error look like?

  • Expected indentation of 1 tab but found 4 spaces indent
  • Expected indentation of 4 spaces but found 1 tab indent

Cause 1 of 3

The rule asks for tabs and the files use spaces (or the reverse)

1Check

The indent rule takes a number for spaces or the string tab. ESLint documents a default of 4 spaces when no value is given.

grep -rn "indent" eslint.config.* .eslintrc* 2>/dev/null

2Fix

Set the value to the style your files use, or keep the rule and convert the files. Many problems this rule reports can be fixed automatically.

// eslint.config.js
export default [
  { rules: { indent: ['error', 2] } },   // or ['error', 'tab']
]

3Verify

Run ESLint with the fix option, then lint again and see no indent errors.

npx eslint --fix .
npx eslint .

Confirmed by eslint.org

Did this fix it?

Cause 2 of 3

VS Code detects indentation and re-indents with the wrong style

1Check

VS Code auto-detects each file's indentation, and the detected value overrides your default settings.

# VS Code shows the detected indentation on the right side of the status bar

2Fix

Set the editor to your project's style, and turn off detection if you want your settings to win. Use the status bar entry to convert a file.

// .vscode/settings.json
{
  "editor.detectIndentation": false,
  "editor.insertSpaces": true,
  "editor.tabSize": 2
}

3Verify

Add a new indented line and lint the file.

npx eslint src/index.js

Confirmed by code.visualstudio.com

Did this fix it?

Cause 3 of 3

The rule is deprecated and a style plugin takes over

1Check

ESLint deprecated this rule in v8.53.0 and plans to remove it in v11.0.0. It points to the matching rule in @stylistic/eslint-plugin.

npm ls eslint @stylistic/eslint-plugin

2Fix

Move the indent setting to the stylistic plugin, or hand formatting to a formatter and drop the rule.

npm install --save-dev @stylistic/eslint-plugin
// eslint.config.js
import stylistic from '@stylistic/eslint-plugin'
export default [{ plugins: { '@stylistic': stylistic }, rules: { '@stylistic/indent': ['error', 2] } }]

3Verify

Remove the old rule, lint again, and confirm only one indent rule reports.

npx eslint .

Confirmed by eslint.org

Did this fix it?

How do I stop it coming back?

  • Write the indent style in the repository: ESLint rule and editor settings agree.
  • Run eslint --fix in a pre-commit step so style drift never builds up.
  • Use one tool for formatting; do not let two tools set indentation.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.