--fix不能修复使用eslint时的错误

48

我在Visual Code中使用ESLint格式化JS文件,但每次运行时都会出现错误。以下是我的命令行: node ./node_modules/eslint/bin/eslint -- src --fix 错误信息如下:

E:\PATH\src\reducers\roote-reducer.js
  1:8   error  There should be no space after '{'                object-curly-spacing
  1:26  error  There should be no space before '}'               object-curly-spacing
  6:1   error  Expected indentation of 1 tab but found 2 spaces  indent
  7:1   error  Expected indentation of 1 tab but found 2 spaces  indent
  8:4   error  Newline required at end of file but not found     eol-last

E:\PATH\src\reducers\schedule-reducer.js
   1:8   error  There should be no space after '{'                  object-curly-spacing
   1:22  error  There should be no space before '}'                 object-curly-spacing
   4:1   error  Expected indentation of 1 tab but found 4 spaces    indent
   7:24  error  Missing space before function parentheses           space-before-function-paren
   7:54  error  Missing space before opening brace                  space-before-blocks
   8:1   error  Expected indentation of 1 tab but found 4 spaces    indent
   9:1   error  Expected indentation of 1 tab but found 4 spaces    indent

E:\PATH\src\register-service-worker.js
   12:1  error  Expected indentation of 1 tab but found 2 spaces    indent
   17:1  error  Expected indentation of 5 tabs but found 6 spaces   indent
   17:7  error  Use regex shorthands to improve readability         unicorn/regex-shorthand
   22:1  error  Expected indentation of 1 tab but found 2 spaces    indent

✖ 309 problems (309 errors, 0 warnings)
  309 errors, 0 warnings potentially fixable with the `--fix` option.

我如何自动修复它们?


请注意,并非所有错误/警告都可以通过 --fix 修复。 - piuspbd
如何修复这些错误/警告? - gretty volk
12个回答

64

要使用--fix选项,您需要直接运行eslint。

尝试这个。

./node_modules/.bin/eslint src --fix

在Windows操作系统上:

.\node_modules\.bin\eslint src\** --fix

7
它不能正确运行,只会显示一个错误(第一个错误),并且没有解决它! - gretty volk
编辑过了,你能详细说明一下问题吗? - piuspbd
请尽量解释一下您的解决方案,以便人们能够理解问题出现的原因以及实际的问题和解决方案是什么。 - Scriptable
4
在执行 --fix 后,将一个目录作为最后一个参数指定,例如:./node_modules/.bin/eslint --fix myJsFiles/,对我很有效。 - Gene Bo
没有必要直接从 ./node_modules/.bin 运行二进制文件。这就是 npxnpm exec 的作用。 - Cameron Tacklind
如果你是 yarn 的爱好者,可以使用 yarn eslint src --fix 命令。之后再感谢我吧! - Joshua Kusaasira

15

格式应该是:

./node_modules/.bin/eslint --fix path/to/file.js

详见此讨论串(以及aravind1078的回答)获取更多背景信息。


我使用了"./node_modules/.bin/eslint --fix src/**.*"命令来对/src目录下的所有文件进行检测和修复。 - walox

11

请在新终端中运行以下命令:

./node_modules/.bin/eslint --fix . --ext .js,.vue src


(注意:保留原文中的HTML标签,但不需要额外的解释或说明)

8
在您的代码所在的目录中执行以下命令:
eslint --fix .

而不是

eslint --fix 

因为该命令需要一个参数来指定代码所在的目录或要应用linting的文件本身


1
我必须明确指定文件,例如 eslint --fix src/path/some.js。上述操作不会扫描目录中的文件。 - armandino

7
尝试运行。
eslint --fix

您需要首先安装 eslint

npm install -g eslint

谢谢。这也解决了我的问题。我遇到了这个问题,尝试了将近一个小时的调试。 - Jose Mhlanga
英雄!顺便说一下,请使用句点“.”结束。非常感谢! - Huthaifa Muayyad

6

使用命令行终端,在Visual Studio Code中运行下面的命令。

确保工作项目配置了 .eslintrc.yml 文件。

要修复文件中的 lint 问题,请执行以下操作:

npx eslint file1.js --fix

为了解决文件夹中所有文件的lint问题。
npx eslint ./folder_name --fix

由于某种原因,这是唯一对我有效的解决方案...非常感谢! - Lily Hahn
3
对我不起作用 - Nienaber

4
  • 在 Nuxt.js 项目中,当我使用 eslint-config-airbnb-base 而不是 @nuxtjs/eslint-module 时,使用 --fix 很困难。
  • 当时直接运行 eslint 对我有用: ./node_modules/.bin/eslint --fix


然而,我更喜欢将两个脚本写入 package.json

"lint": "eslint --ext \".js,.vue\" --ignore-path .gitignore .",
"lint-fix": "npm run lint -- --fix"

然后运行 yarn lint-fixnpm run lint-fix


3

如果你还没有安装eslint,请先安装它

npm install -g eslint

然后运行

eslint --fix path/to/file.extension

注意:每次对特定文件进行更改后,都必须运行上述命令。

2
如果它没有

extend(config, ctx) {
      // Run ESLint on save
      if (ctx.isDev && ctx.isClient) {
        config.module.rules.push({
          enforce: 'pre',
          test: /\.(js|vue)$/,
          loader: 'eslint-loader',
          exclude: /(node_modules)/,
          options: {
            fix: true
          }
        })
      }
    }
You. You can use this on the first page
    const colors = require('vuetify/es5/util/colors').default
    const pkg = require('./package')
    require('dotenv').config()

    module.exports = {
      mode: 'universal',
      /*
      ** Headers of the page
      */
      head: {
        titleTemplate: '%s - ' + process.env.npm_package_name,
        title: process.env.npm_package_name || '',
        meta: [
          { charset: 'utf-8' },
          { name: 'viewport', content: 'width=device-width, initial-scale=1' },
          {
            hid: 'description',
            name: 'description',
            content: process.env.npm_package_description || ''
          }
        ],
        link: [{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }]
      },
      /*
      ** Customize the progress-bar color
      */
      loading: { color: '#fff' },
      /*
      ** Global CSS
      */
      css: [],
      /*
      ** Plugins to load before mounting the App
      */
      plugins: [],
      /*
      ** Nuxt.js dev-modules
      */
      buildModules: [
        // Doc: https://github.com/nuxt-community/eslint-module
        '@nuxtjs/eslint-module',
        '@nuxtjs/vuetify'
      ],
      /*
      ** Nuxt.js modules
      */
      modules: [
        // Doc: https://axios.nuxtjs.org/usage
        '@nuxtjs/axios',
        '@nuxtjs/pwa',
        // Doc: https://github.com/nuxt-community/dotenv-module
        '@nuxtjs/dotenv'
      ],
      /*
      ** Axios module configuration
      ** See https://axios.nuxtjs.org/options
      */
      axios: {},
      /*
      ** vuetify module configuration
      ** https://github.com/nuxt-community/vuetify-module
      */
      /*
      ** Build configuration
      */
      build: {
        extend(config, ctx) {
          // Run ESLint on save
          if (ctx.isDev && ctx.isClient) {
            config.module.rules.push({
              enforce: 'pre',
              test: /\.(js|vue)$/,
              loader: 'eslint-loader',
              exclude: /(node_modules)/,
              options: {
                fix: true
              }
            })
          }
        }
      }
    }

如果您仍然不理解我。


1
这解决了我Nuxt项目中的问题,非常感谢。 - Annamalai D

1
我刚刚遇到了同样的问题,我运行了。
eslint --fix fileName.extention

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接