【问题标题】:--fix doesn't fix the errors using eslint--fix 不使用 eslint 修复错误
【发布时间】:2018-12-18 07:32:15
【问题描述】:

我在可视化代码中使用 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修复
  • 如何解决这个错误/警告?

标签: javascript reactjs visual-studio editor eslint


【解决方案1】:

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

试试这个

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

在 Windows 上:

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

【讨论】:

  • 它不能正常工作,它只给出一个错误(第一个错误),并没有解决它!
  • 已编辑,您能详细介绍一下这个问题吗?
  • 请尝试解释一下您的解决方案,以便人们了解问题发生的原因以及实际问题和解决方案是什么。
  • --fix 之后将目录指定为最后一个参数后,这对我有用,例如:./node_modules/.bin/eslint --fix myJsFiles/。谢谢
  • 无需直接从./node_modules/.bin 运行二进制文件。这就是npxnpm exec 的用途。
【解决方案2】:

格式应该是:

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

有关更多背景信息,请参阅 this thread(和 aravind1078 的回答)。

【讨论】:

  • 我使用 ./node_modules/.bin/eslint --fix src/**.* 在 /src 内的所有文件上运行
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

    尝试运行

    eslint --fix
    

    你必须先安装 eslint

    npm install -g eslint
    

    【讨论】:

    • 谢谢。这也解决了我的问题。遇到这个并尝试调试了将近 1 小时
    • 英雄!我可以在最后使用. 添加吗?非常感谢!
    【解决方案5】:

    在您的代码所在的目录中运行:

    eslint --fix .
    

    而不是

    eslint --fix 
    

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

    【讨论】:

      【解决方案6】:

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

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

      修复文件中的 lint 问题

      npx eslint file1.js --fix
      

      修复文件夹中所有文件的 lint 问题

      npx eslint ./folder_name --fix
      

      【讨论】:

      • 出于某种原因,这是唯一对我有用的解决方案...非常感谢!
      • 对我不起作用
      【解决方案7】:

      如果没有

      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
                    }
                  })
                }
              }
            }
          }

      If you still don't understand me.

      【讨论】:

      • 这解决了我的 Nuxt 项目中的问题,非常感谢
      【解决方案8】:

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

      npm install -g eslint
      

      然后运行

      eslint --fix path/to/file.extension
      


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

      【讨论】:

        【解决方案9】:

        我刚刚遇到了同样的问题

        eslint --fix fileName.extention
        

        【讨论】:

          【解决方案10】:

          第一个安装包: npm install -g eslint

          运行: eslint --fix

          【讨论】:

            【解决方案11】:
            • 当我在 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 运行 lint -- --fix"

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

            【讨论】:

              猜你喜欢
              • 2020-08-27
              • 2021-02-05
              • 1970-01-01
              • 1970-01-01
              • 2019-12-24
              • 2016-10-15
              • 2018-05-03
              • 2019-05-22
              • 2019-06-07
              相关资源
              最近更新 更多