33

I have no clue what's going on,

I cloned a github repo and literally just tried to change like one line but I got hit by this prettier error which makes no sense to me (I've never used prettier).

Replace ↹return·(⏎↹↹<img·alt='logo'·src='./Logo.png'·/>⏎↹); with ··return·<img·alt="logo"·src="./Logo.png"·/> prettier/prettier

Anything could be helpful at this point, I'm using MacOS and working on VSCode

6
  • 2
    Looks like it just wants you to fix the non-standard whitespace issues, tabs or whatever into spaces, and also remove the unnecessary parentheses.
    – Drew Reese
    Commented May 26, 2021 at 9:32
  • 2
    @DrewReese is there a way to just disable it? I don't really need all this perfection right now
    – Pepe
    Commented May 26, 2021 at 9:38
  • 2
    Sure, search in the VSCode settings (cmd + shift + P), or more likely, look for a .prettierrc.json (or similar) file in the root of the project. Probably about as much effort to search the settings and enable the format on save.
    – Drew Reese
    Commented May 26, 2021 at 9:41
  • @DrewReese I'm sorry but I really can't do it, I disable it from the setting and it keeps going, I tried from the .prettierrc.json but it keep going, it just keep giving me errors for formatting stuff (Not that I can't fix them, I just want to be free from this cancer of prettier)
    – Pepe
    Commented May 26, 2021 at 10:38
  • Can you do a global search (cmd + shift + f) for "prettier" and see what results you have, there may be some some settings your eslint configs. Can you update your question to include your .prettierrc.json and eslintrc.json configuration files if available?
    – Drew Reese
    Commented May 26, 2021 at 15:46

13 Answers 13

29

Let's see why we are getting this error.

  1. If you have installed the prettier extension in your VSCode then the default values are set as mentioned here which can be seen by searching prettier in the settings of VSCode as well.

  2. Now if you have enabled formatOnSave in your VSCode the prettier formats your code based on configs from the VSCode.

  3. This error would occur when the configs from the VSCode conflicts from the configs mentioned in .prettierrc.json or .eslintrc.json.

    enter image description here

Ex: Let's say your project is using a printWidth of 100 but the default printWidth is 80. ( Search prettier printwidth in VSCode settings )

In general the spacing errors will be autoCleared ( autoFormatted ) on save by prettier. But in this case that won't work.

Reason: Prettier is sticking to the config ( printWidth: 80 ) which is an error according to Repo's eslintrc/ prettierrc ( printWidth: 100 )

Fix here

  1. Change default VSCode Prettier configs. ❌ -> This would be a bad idea as it will effect all your projects opened in VSCode.

  2. Better way to fix this issue is by adding a .vscode/settings.json in the root directory of the repo.

  3. Add these lines in the settings.json

    {
      "editor.codeActionsOnSave": { "source.fixAll": true },
      "editor.formatOnSave": false,
    } 
    
  4. Now go to files with errors and save the files to format. Files will be formatted according to the configs mentioned in project's eslintrc/ prettierrc

  5. Instead of going to each file you can fix all autofixable problems from the command line as below.

  6. Go to package.json and add this line to your scripts.

    "lint-fix": "eslint --fix 'src/**/*.{js,jsx,ts,tsx,json,css,scss,md}'",

  7. Now in the terminal run npm run lint-fix.

2
  • Thanks! It worked for me. I understand the difference between codeActionsOnSave and formatOnSave as explained in 'stackoverflow.com/questions/60718493/…'.
    – taoliujun
    Commented Feb 27, 2023 at 3:44
  • what finally worked for me was running npm run lint -- --fix, I tried just about every fix given in all the answers, including this one but none of the answers worked for me... Hopefully some other hapless soul may find this useful, took me the better part of 3 hours to fix this ludicrous issue... Commented Apr 24 at 19:33
12

I had the same issue, in the eslinrc.json file under "prettier/prettier", I removed printWidth.

2
  • 4
    This is not the proper solution.
    – manojadams
    Commented Feb 21, 2023 at 6:41
  • agree with him about it that Is not proper solution
    – LOGAN
    Commented Jun 28 at 7:52
6

You can just set/update this rule in your .eslintrc.js

rules: {
  ...,
  'prettier/prettier': ['error', { printWidth: 120 }],
}

By default, printWidth is 80. ESLint wants to break down the line to match this length requirement that can be overwitten.

1
  • This answer fixed my problem, i have PrintWidth at the default 80 but Eslint wanted to combine lines (usually in imports) and Prettier wanted to break them up. Causing a never ending conflict dance when linting and formatting I set the the explicit prettier printWidth rule in Eslint as: 'prettier/prettier': ['error', { printWidth: 80 }] and the error goes away!
    – Chrispy
    Commented Apr 2 at 20:43
5

This is usually due to some configuration with eslint preventing you from making errors and introducing unwanted characters in your code. I fixed this by running one line eslint --fix . . Make sure you install eslint globally first npm i -g eslint .

3

I think this is caused by Prettier being configured to use spaces instead of tabs to indent and then your code editor using tabs. So Prettier wants you to replace those tabs with spaces.

Alternatively, you can set your code editor to use tabs.

What worked for me was adding this to the rules object in .prettierrc:

{
  "useTabs": false
}
0

I had the same issue, I changed the tab width inside the prettier formatter extension to the configured size.

0

I added in the .prettierc props "endOfline".

{
  "endOfLine": "lf",
}

By default Windows uses CRLF line separator, so you can cahnge it through a global config of IDE to use LF instad of CRLF

0

I figured the formatting issue (VS Code):

  1. settings.json:

    "editor.codeActionsOnSave": { "source.fixAll.eslint": true }, "editor.defaultFormatter": "dbaeumer.vscode-eslint", "editor.formatOnSave": true

  2. .eslintrc.json: "useTabs": false}

  3. .editorconfig: indent_style = space

  4. if esbenp.prettier-vscode is enabled in VSCode - please disable it.

See my complete eslint + prettier setup for this repo: https://github.com/Sgryts/ng2-feature-toggle

0

In my case eslint was preventing Prettier to format the file while saving (I have set my VSCode to format the document upon saving).

Due that, Prettier was complaining about tabs / spaces being added when it was not required.

To fix I had to first fix eslint by using the below command(make sure to install the package globally):

eslint --fix

and then I had to Save the files again so Prettier could format them correctly.

0

Select all, right click, and select format selection

0

Try using the following settings:

.editorconfig file

indent_style = tab
indent_size = 2

.pretteierrc file

"tabWidth": 2

Note: reopen vscode after updating this setting. Also if you have .eslintrc file, make sure that prettier is included in the extends array like this:

"extends": ["standard",..,"prettier"]
0

You just can run pnpm prettier --write . to fix this

-1

For me I had to remove

"extends": "eslint:recommended", 

from the .eslintrc.js file in the project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.