简体   繁体   English

--fix 不使用 eslint 修复错误

[英]--fix doesn't fix the errors using eslint

I'm using eslint in visual code to format the js files but it gives me errors everytime i run it, here's my command line to run it node ./node_modules/eslint/bin/eslint -- src --fix the error message我在可视化代码中使用 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.

How can i fix them automatically?我怎样才能自动修复它们?

To use --fix option, you need to run eslint directly.要使用--fix选项,您需要直接运行 eslint。

Try this尝试这个

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

On Windows:在 Windows 上:

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

The format should be:格式应为:

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

See this thread (and aravind1078's answer) for more background.有关更多背景信息,请参阅此线程(和 aravind1078 的答案)。

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

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

Try to run尝试运行

eslint --fix

you have to install first eslint你必须安装第一个 eslint

npm install -g eslint

in the directory in which you code exists run :在您的代码存在的目录中运行:

eslint --fix .

instead of代替

eslint --fix 

as the command needs an argument which indicates the directory in which the code exists or the file itself you want to apply linting on因为该命令需要一个参数,该参数指示代码所在的目录或要对其应用 linting 的文件本身

Use Command-Line Terminal, to run the below commands in Visual Studio Code.使用命令行终端,在 Visual Studio Code 中运行以下命令。

Make sure .eslintrc.yml file configured for the working project确保为工作项目配置了 .eslintrc.yml 文件

To fix lint issues in a file修复文件中的 lint 问题

npx eslint file1.js --fix

To fix lint issues in all the files in the folder修复文件夹中所有文件的 lint 问题

npx eslint ./folder_name --fix

If it didn't如果没有

 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. 如果你还是不明白我。

First install eslint if you already haven't如果您还没有安装 eslint,请先安装

npm install -g eslint

Then run然后运行

eslint --fix path/to/file.extension


Note: You will have to run the the above command every time after making changes to that particular file.注意:每次更改该特定文件后,您都必须运行上述命令。

  • I had difficulties in using --fix when I use eslint-config-airbnb-base instead of @nuxtjs/eslint-module in Nuxt.js project当我在 Nuxt.js 项目中使用eslint-config-airbnb-base而不是@nuxtjs/eslint-module时,我在使用--fix时遇到了困难
  • At that time running eslint direclty worked for me: ./node_modules/.bin/eslint --fix当时运行 eslint 直接对我有用:./node_modules/.bin/ ./node_modules/.bin/eslint --fix


However, my preferred one is to write two more scripts into package.json但是,我更喜欢在package.json中再写两个脚本

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

And run yarn lint-fix or npm run lint-fix并运行yarn lint-fixnpm run lint-fix

我刚刚遇到了同样的问题

eslint --fix fileName.extention

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

and

Run: eslint --fix运行:eslint --fix

if it doesn't fix any error with the '--fix' flag, it means your ESlint config file has an error.如果它没有用 '--fix' 标志修复任何错误,这意味着你的 ESlint 配置文件有错误。 please cross check the file again请再次交叉检查文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM