【问题标题】:Why do I keep getting Delete 'cr' [prettier/prettier]?为什么我不断收到 Delete 'cr' [prettier/prettier]?
【发布时间】:2025-11-24 20:10:02
【问题描述】:

我在 Prettier 1.7.2 和 Eslint 1.7.0 中使用 vscode。在我得到的每个换行符之后:

[eslint] Delete 'cr' [prettier/prettier]

这是.eslintrc.json:

{
  "extends": ["airbnb", "plugin:prettier/recommended"],
  "env": {
    "jest": true,
    "browser": true
  },
  "rules": {
    "import/no-extraneous-dependencies": "off",
    "import/prefer-default-export": "off",
    "no-confusing-arrow": "off",
    "linebreak-style": "off",
    "arrow-parens": ["error", "as-needed"],
    "comma-dangle": [
      "error",
      {
        "arrays": "always-multiline",
        "objects": "always-multiline",
        "imports": "always-multiline",
        "exports": "always-multiline",
        "functions": "ignore"
      }
    ],
    "no-plusplus": "off"
  },
  "parser": "babel-eslint",
  "plugins": ["react"],
  "globals": {
    "browser": true,
    "$": true,
    "before": true,
    "document": true
  }
}

.prettierrc 文件:

{
  "printWidth": 80,
  "tabWidth": 2,
  "semi": true,
  "singleQuote": true,
  "trailingComma": "es5",
  "bracketSpacing": true,
  "jsxBracketSameLine": false,
}

我怎样才能摆脱这个错误?

【问题讨论】:

  • 看看你的 .eslintrc.js 文件。从 extends 数组中删除 'plugin:prettier/recommended' 应该可以解决问题。
  • 为什么要扩展"plugin:prettier/recommended"

标签: reactjs visual-studio-code prettier eslintrc


【解决方案1】:

尝试在 .prettierrc 文件中设置"endOfLine":"auto"(在对象内部)

或设置

'prettier/prettier': [
  'error',
  {
    'endOfLine': 'auto',
  }
]

在 eslintrc 文件的 rules 对象中。

如果您使用的是 windows 机器 endOfLine 可以根据您的 git 配置“crlf”。

【讨论】:

  • 更改 .eslintrc 文件对我有用,但不适用于 .prettierrc 文件。不知道为什么或有什么区别(我对 OP 上的所有标签都是新手)。
  • 我猜你可能需要在 VS Code 中使用 Prettier 扩展。 prettierrc 仅在这种情况下有效。
  • 在 Windows 机器上将行尾从 CRLF 更改为 LF 对我有用
  • 对于像我这样的新手来说,应该这样做。打开根目录中的.eslintrc.json (frontend)。更改后将如下所示:{ "extends": ["react-app", "prettier"], "plugins": ["prettier"], "rules": { "prettier/prettier": ["error", { "endOfLine": "auto" }] } }
  • endOfLine: 'auto' 更新 .prettierrc.js 在 windows + vs 代码上为我工作。更新 .eslintrc.js 无效
【解决方案2】:

在 VSCode 上更改此设置。

【讨论】:

  • 这可以解决问题,但前提是您使用 CRLF 打开了其他源文件。以上答案更有效。
  • 这对我有用。我确实尝试了其他方法,包括编辑配置文件,但都没有奏效。
  • 这拯救了我的一天。我正在将 VS Code 与 Window 一起使用。这可能是由于来自其他系统的相同提交
  • 火箭科学!
  • 除了在 VSCode 中将 CRLF 更改为 LFgit 可能会在后台进行自动转换。如果您在安装时选择了checkout Windows-style,它会将您克隆的源代码转换为CRLF。所以重新安装git并选择checkout as-is, commit Unix-style会修复它。
【解决方案3】:

我用的是git+vscode+windows+vue,看了eslint文档后:https://eslint.org/docs/rules/linebreak-style

最后修复它:

*.js text eol=lf 添加到.gitattributes

然后运行vue-cli-service lint --fix

【讨论】:

    【解决方案4】:

    在我的 Windows 机器中,我通过在当前项目目录中存在的 .eslintrc.js 文件的 rules 对象中添加以下代码 sn-p 解决了这个问题。

        "prettier/prettier": [
          "error",
          {
            "endOfLine": "auto"
          },
        ],
    

    这也适用于我的 Mac

    【讨论】:

    • @brijs 你是什么意思?
    【解决方案5】:

    我知道这是旧的,但我只是在我的团队中遇到了这个问题(一些 mac,一些 linux,一些 windows,所有 vscode)。

    解决方法是设置以 vscode 的设置结尾的行:

    .vscode/settings.json

    {
        "files.eol": "\n",
    }
    

    https://qvault.io/2020/06/18/how-to-get-consistent-line-breaks-in-vs-code-lf-vs-crlf/

    【讨论】:

      【解决方案6】:

      我升级到“更漂亮”:“^2.2.0”并且错误消失了

      【讨论】:

        【解决方案7】:

        在 .eslintrc.json 文件中 在辅助角色中添加此代码将解决此问题

              "rules": {
            "prettier/prettier": ["error",{
              "endOfLine": "auto"}
            ]
        
          }
        

        【讨论】:

          【解决方案8】:

          试试这个。它对我有用:

          yarn run lint --fix

          npm run lint -- --fix

          【讨论】:

          • 这可能有助于npm run lint -- --fix
          • @Cláudio 试试这个 eslint --ext js,jsx,ts,tsx src --quiet --fix > lint.log
          【解决方案9】:

          已修复 - 我的 .eslintrc.js 看起来像这样:

          module.exports = {
            root: true,
            extends: '@react-native-community',
            rules: {'prettier/prettier': ['error', {endOfLine: 'auto'}]},
          };
          

          【讨论】:

          • 非常感谢!即使我更改了 LF 和 CRLF,对我也很有效。
          【解决方案10】:

          在根目录中打开 .editorconfig 文件并进行更改:

          end_of_line = lf

          end_of_line = auto

          这应该会为新文件修复它。

          【讨论】:

            【解决方案11】:

            将此添加到您的 .prettierrc 文件并打开 VSCODE

            "endOfLine": "auto"
            

            【讨论】:

              【解决方案12】:

              以上所有答案都是正确的,但是当我使用 windows 并禁用 Prettier ESLint 扩展rvest.vs-code-prettier-eslint时,问题将得到解决。

              【讨论】:

                【解决方案13】:

                从 tsx -> ts, jsx -> js 更改文件类型

                如果您正在处理 .tsx 或 .jsx 文件并且您只是导出样式等而不是 jsx,则可能会出现此错误。在这种情况下,可以通过将文件类型更改为 .ts 或 .js 来解决错误

                【讨论】:

                  【解决方案14】:

                  对我有用的是:

                  1. 按照 Roberto LL 的建议,将 prettier 更新到 2.2.1 版(目前是最新版本)。执行它

                  npm 更新更漂亮

                  1. 按照 Hakan 的建议执行 lint fix(这将修改项目中的所有文件以将行结尾转换为 LF)。

                  npm run lint -- --fix

                  无需更改 .eslintrc 和 .prettierrc 文件!

                  【讨论】:

                  • prettier 在我的机器上是最新的,但是运行 npm run lint -- --fix 解决了这个问题。
                  • 这适用于我的 Nestjs 和 windows,谢谢!
                  • 我尝试了这里提出的许多其他解决方案,但只有这个解决了问题。
                  【解决方案15】:

                  没有必要忽略 linter 的规则。自动修复它

                  npm install -g prettier
                  prettier -w .\webpack.config.js # or other file
                  

                  【讨论】:

                    【解决方案16】:

                    修复:我的 eslintrc.js 的一些规则如下所示:

                    rules: {
                        'prettier/prettier': ['error', { "endOfLine": "auto"}, { usePrettierrc: true }],  // Use our .prettierrc file as source
                        'react/react-in-jsx-scope': 'off',
                        'react/prop-types': 'off',
                        'simple-import-sort/imports': 'error',
                        "simple-import-sort/exports": "error"
                    }
                    

                    【讨论】:

                      【解决方案17】:

                      在我的情况下,我在 Mac 中使用 Windows 操作系统和 git 代码支持并转换为 CRLF 在 cmd 提示符下运行以下命令以停止将文件转换为 CRLF:

                      git config --global core.autocrlf input
                      

                      再次签出代码并再次打开 Visual Studio Code 并再次运行您的脚本。它对我有用。

                      【讨论】:

                        【解决方案18】:

                        我在这里尝试了一切,对我来说,我需要通过图标扩展管理更漂亮的配置扩展 > 更漂亮 > 小引擎 > 扩展设置 > 更漂亮:行尾 > 设置为自动。

                        在我的 settings.json 中添加这两行之后

                        "eslint.run": "onSave",
                        "editor.formatOnSave": true,
                        

                        我能够在 .eslintrc.js 规则中使用下面的配置。

                        "prettier/prettier": ["error", {
                            "endOfLine":"auto"
                        }],
                        

                        【讨论】:

                          【解决方案19】:

                          解决方案

                          git config --global core.autocrlf false
                          

                          全局配置后,需要再次拉取代码。

                          问题的根本原因:

                          罪魁祸首是gitcore.autocrlf的配置属性

                          由于历史原因,windowslinux上的文本文件的换行符不同。

                          • Windows换行时同时使用回车CR(carriage-return character)和换行LF(linefeed character)
                          • MacLinux 只使用换行符 LF
                          • 旧版Mac使用回车CR

                          因此,在不同系统中创建和使用文本文件时,会出现不兼容的问题。

                          当我在Windows 上克隆代码时,autocrlf 默认为 true,然后文件的每一行都会自动转换为 CRLF。如果您不对文件进行任何更改,eslint 删除 CR by pre-commit 因为git 自动将CRLF 转换为LF

                          参考

                          https://developpaper.com/solution-to-delete-%E2%90%8Deslint-prettier-prettier-error/

                          【讨论】:

                          • 我遇到问题的项目是在 macos 上开发的,而不是在 windows 上安装会导致上述错误。这个解决方案是我认为明智的,它解决了这个问题。无需更改代码,项目运行没有错误。
                          • 这对我有用...命令 + repull 存储库。
                          【解决方案20】:

                          如果上面的代码不适合你,试试这两个步骤。

                          1。 在文件 .eslintrc.json 在规则对象内添加此代码将解决此问题

                           "prettier/prettier": ["error",{
                                "endOfLine": "auto"}
                              ]
                          

                          2 更改开发服务器--fix

                          npm run dev
                          

                          npm run dev --fix
                          

                          npm run lint -- --fix
                          yarn run lint -- --fix
                          

                          【讨论】:

                          • 你的意思是inside rules object吗?如果是这样,请解决这个问题
                          【解决方案21】:

                          检查 VS Code 底部状态栏的右侧,其中显示了行和列、空格、文本编码(UTF-8 等)等信息。您应该会看到一个Select End Of Line Sequence 状态显示(LFCRLF),您可以单击它进行更改。确保您没有手动将其更改为与您希望 Prettier 使用的内容相冲突的内容。

                          【讨论】:

                            【解决方案22】:

                            我在我的 Nest js 应用程序中遇到了同样的问题。将以下代码添加到.eslintrc.jsrules 然后运行yarn run lint --fix 解决了这个问题。

                            'prettier/prettier': [
                              'error',
                              {
                                endOfLine: 'auto',
                              },
                            ],
                            

                            我的.eslintrc.js 规则看起来像这样..

                             rules: {
                            '@typescript-eslint/interface-name-prefix': 'off',
                            '@typescript-eslint/explicit-function-return-type': 'off',
                            '@typescript-eslint/explicit-module-boundary-types': 'off',
                            '@typescript-eslint/no-explicit-any': 'off',
                            'prettier/prettier': [
                              'error',
                              {
                                endOfLine: 'auto',
                              },
                            ],
                            

                            },

                            【讨论】:

                              【解决方案23】:

                              在 .eslintrc 文件中添加以下规则,然后重新启动您的项目。

                              rules: {
                                  'prettier/prettier': ['error', { "endOfLine": "auto"}, { usePrettierrc: true }],  
                              }
                              

                              【讨论】:

                                【解决方案24】:

                                解决方案

                                1.禁用自动转换 git 设置

                                git --global config core.autocrlf false

                                2。删除旧缓存数据

                                git rm --cached -r .

                                3.重置 git 文件

                                git reset --hard

                                【讨论】:

                                  【解决方案25】:

                                  当我从 git 中提取代码时出现错误,这对我有用:

                                  第 1 步:

                                  git config --global core.autocrlf false

                                  第 2 步:

                                  • 删除当前文件夹

                                  第 3 步:

                                  • 再次从 git 拉取代码
                                  • 再次尝试运行命令

                                  【讨论】:

                                    【解决方案26】:

                                    对我有用

                                    第 1 步 React js根目录查找.eslintrc文件

                                    第 2 步 在 .eslintrc 中查找

                                    "prettier/prettier": [
                                      2,
                                      {
                                        "printWidth": 100,
                                        "singleQuote": true,
                                        "trailingComma": "none",
                                        "tabWidth": 2
                                      }
                                    ]
                                    

                                    替换为

                                    "prettier/prettier": [
                                      "error",
                                      {
                                        "endOfLine": "auto"
                                      }
                                    ]
                                    

                                    保存文件然后运行

                                    npm start
                                    

                                    【讨论】:

                                      【解决方案27】:

                                      编辑您的 .eslintrc.json 文件并更新如下所示的“prettier/prettier”值。

                                      我面临同样的问题并使用以下更改进行了修复。

                                      "prettier/prettier": [
                                          "error", {
                                               "singleQuote": true,
                                               "parser": "flow" 
                                          }
                                      ],
                                      

                                      【讨论】:

                                        【解决方案28】:

                                        在 .eslintrc 文件中添加以下内容:

                                        • extends: ['prettier']plugins: ['prettier']

                                        • rules: {'prettier/prettier': ['error', {endOfLine: 'auto'}]}

                                        在 .prettierrc 中删除:

                                        • endOfLine: 'auto'

                                        它对我有用。

                                        【讨论】:

                                          【解决方案29】:

                                          如果您已经签出代码

                                          git config --global core.autocrlf input 
                                          
                                          
                                          git rm --cached -r . 
                                          
                                          
                                          git reset --hard
                                          

                                          【讨论】:

                                            【解决方案30】:

                                            npm run lint -- --fix

                                            运行这个对我有用的命令

                                            【讨论】: