Everytime I run 'create-react-app', my initialized project is using 2 instead of 4 indentation spaces. How to automatically convert to 4 tabs?
I'm using Atom as a text editor. Under 'config.cson', the tab length is always set to 4.
Everytime I run 'create-react-app', my initialized project is using 2 instead of 4 indentation spaces. How to automatically convert to 4 tabs?
I'm using Atom as a text editor. Under 'config.cson', the tab length is always set to 4.
There isn't a way to tell create-react-app to indent using 4 spaces instead of 2. This is a style that the creators of React have standardized on.
The best you can do is do a global search and replace in your source files for two spaces and replace it with four spaces.
I added an .eslintrc (in the project root folder, but it could be in ./src as eslint settings used are those closer to whatever is getting linted) with the following rules (be warned - these parser/parserOption rules seem to change rapidly). https://eslint.org/docs/rules/indent
{
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
},
"rules": {
"indent": ["error", 4, { "SwitchCase": 1 }]
}
}
Then, I ran with this command: eslint ./src/ --fix
Basically, I'm overriding whatever nested/hidden eslint settings that create-react-app comes with by default. I'm using VSCode and found I had to re-open files to get the changes to show (with visual helpers I use like indent guides).
NOTE: Edited for "SwitchCase" addition. Without this, as of today, cases get no indent in a switch statement (visually in same column as keyword "switch") per create-react-app defaults. You can just leave out that extra object in the "indent" array if you like that.
Error: Cannot find module 'babel-eslint'
–
Arnettearney Run the following commands, (For VS Code)
First of all install Prettier extension in VS Code.
cd ~/my-project
(Go to your current directory and open terminal)npm install --global prettier
npm install --save-dev prettier
npx prettier --write --tab-width 4 --single-quote "**/*"
Then reopen VS Code the changes should reflect.
If you find error installing command 2 use this instead,
sudo npm install --global prettier
.
This will fix the issue in case it occurs.
Not certain about create-react-app specifically but this should help configure your indentations app wide
You can choseyour editor and make the necessary configuration changes on your project to maintain common spacing and identation project wide - regardless of who is using which editor.
You can perfectly override those abusive settings (I call abusive to something that is not meant for you to customize even when you dedicate long hours to work on it),
Just go to VSCode -> Settings, type 'Indentation' in the search box and find the option 'Auto Indent' ('Auto Indent' will appear at the top of the list) and set it to 'none'.
That works for 'create-react-native' app with Expo in VSCode for Mac being September 2021.
If it's VSCode, go to File-Preferences-Settings and you need to find 'Detect Indentation' and uncheck it. After that, check 'Tab Size'. There should be the number 4.
© 2022 - 2025 — McMap. All rights reserved.