'Eslint expected indentation of 1 tab but found 4 spaces error
I am using VScode with latest version of Eslint. It is my first time using a linter.
I keep getting this linting error when using a tab as indentation:
severity: 'Error' message: 'Expected indentation of 1 tab but found 4 spaces. (indent)' at: '4,5' source: 'eslint'
Here is my config file
{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"rules": {
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}
I don't understand why this error is being thrown as I indicated tabs for indentation. It is obviously calculating my 1 tab as 4 spaced but I don't understand why it is doing that when I am pressing tab for indentation.
update: The reason is because in VScode using ctrl + shift + i to beautify code will actually use spaces and not tabs. That is the reason.
Solution 1:[1]
In VSCODE, go to menu:
File / Preferences / Settings then Text Editor (or just search for 'tab' in the search settings box)
Then remove the tick from the following settings:
Insert SpacesDetect Indentation
Also to auto format the document with the default VSCode keyboard shortcut use:
Shift+Alt+f
Solution 2:[2]
Try to disable indent inside .eslintrc.js file
rules: {
'indent': 'off'
}
this works fine for me
Solution 3:[3]
If you use both eslint and prettier, don't disable indent by using {'indent': 'off'} in rules object. To ensure the consistency of your code style, you have to use this rule.
Solution:
This issue is probably happened because of eslint & prettier conflict.
Try to play with different options of eslint in .eslintrc file.
If you hover the error lines in vsCode, at the end of error description you can click on that link to see eslint docs.
For example, in case of indent docs is in this link:
For me, error resolved by adding this line (for ternary expressions):
...
rules: {
indent: [
'error',
2,
{
SwitchCase: 1,
ignoredNodes: ['ConditionalExpression'], <-- I add this line
},
],
...
You can also try flatTernaryExpressions or offsetTernaryExpressions for ternary expressions.
Solution 4:[4]
I used VScode to solve this problem. All you have to do is hold the mouse over the part where there is an error.

and...

Solution 5:[5]
You can automaticaly fix the problems with
npm run lint -- --fix
Solution 6:[6]
Wee, that exactly what it says. You have in your config "indent": [ "error", "tab" ], So it expects tab as indent. But found in your file 4 spaces. Remove spaces and put tab in you file
Solution 7:[7]
I had a similar problem and solved with this code:
rules: {
...
indent: [2, "tab"],
"no-tabs": 0,
...
}
Solution 8:[8]
I was having the same problem and I solved my problem with documentation. Instead of disabling eslint indent, you can add it as shown in the documentation.
Simple:
rules: {
indent: ['error', 2, { "MemberExpression": 1 }],
}
Solution 9:[9]
In file
settings.json
remove this line if have:
"editor.defaultFormatter": "esbenp.prettier-vscode",
eslint conflict with prettier
Solution 10:[10]
If you are using VSCODE follow the next steps.
- Access the settings by clicking: code > preferences > settings, as shown in the following image.
- In the settings, click: Text Editor after that, uncheck the Insert Space option and the Detect Indentation option as shown in the following image.
- Restart VSCODE and your dev server.
Solution 11:[11]
It worked for me, if error is coming then just solve it line by line simply in your code, like : 1.)Expected indentation of 2 spaces but found 8 -> then put only 2 spaces from the starting of the line 2.)Unexpected tab character -> don't use tabs, use spaces 3.)Trailing spaces not allowed -> don't give any spaces after lines end. 4.)Missing space before value for key 'name' -> put 1 space after ":" in object value 5.)A space is required after ',' -> put 1 space after "," in parameter of the function 6.)Opening curly brace does not appear on the same line as controlling statement -> just put the opening curly brace where function starts in the same line 7.)Closing curly brace should be on the same line as opening curly brace or on the line after the previous block -> put the closing curly brace just below where the function starts.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow


