如何修复eslint配置?

How to fix eslint configuration?

我有 node.js 项目,并从 eslint 配置开始。 有.eslintrc.json

{
  "parser": "babel-eslint",
  "extends": [
    "airbnb",
    "plugin:node/recommended",
    "plugin:security/recommended",
    "prettier",
  ],
  "env": {
    "node":true,
    "commonjs": true,
    "es6": true,
  },
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "plugins": [
    "prettier"
  ],
  "rules": {
    "prettier/prettier": "error",
    "node/exports-style": ["error", "module.exports"],
    "node/file-extension-in-import": ["error", "always"],
    "node/prefer-global/buffer": ["error", "always"],
    "node/prefer-global/console": ["error", "always"],
    "node/prefer-global/process": ["error", "always"],
    "node/prefer-global/url-search-params": ["error", "always"],
    "node/prefer-global/url": ["error", "always"],
    "node/prefer-promises/dns": "error",
    "node/prefer-promises/fs": "error",
  },
}

和package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "license": "ISC",
  "lint-staged": {
    "*.{js,jsx}": [
      "eslint --fix",
      "git add"
    ],
    "*.scss": [
      "stylelint --fix",
      "git add"
    ]
  },
  "scripts": {
    "development": "nodemon",
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "babel-node server.js",
    "eslint": "eslint config '**/*.js' --ext .js",
    "prettier": "prettier '**/*.js' --write",
    "stylelint": "stylelint '**/*.js'"
  },
  "author": "",
  "dependencies": {
    "@babel/polyfill": "^7.6.0",
    "body-parser": "^1.19.0",
    "cookie-parser": "^1.4.4",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "helmet": "^3.21.0",
    "history": "^4.9.0",
    "mongoose": "^5.6.13",
    "nodemon": "^1.19.2"
  },
  "devDependencies": {
    "@babel/cli": "^7.6.4",
    "@babel/core": "^7.6.0",
    "@babel/node": "^7.6.3",
    "@babel/plugin-proposal-class-properties": "7.3.3",
    "@babel/plugin-proposal-json-strings": "7.2.0",
    "@babel/plugin-syntax-dynamic-import": "^7.2.0",
    "@babel/plugin-syntax-import-meta": "7.2.0",
    "@babel/preset-env": "^7.3.1",
    "@commitlint/cli": "^7.5.2",
    "@commitlint/config-conventional": "^7.5.0",
    "autoprefixer": "^9.4.7",
    "babel-eslint": "^10.0.3",
    "babel-loader": "^8.0.6",
    "babel-preset-env": "^1.7.0",
    "cross-env": "^5.2.0",
    "eslint": "^6.4.0",
    "eslint-config-airbnb": "^18.0.1",
    "eslint-config-prettier": "^6.3.0",
    "eslint-plugin-import": "^2.18.2",
    "eslint-plugin-prettier": "^3.1.0",
    "husky": "^1.3.1",
    "lint-staged": "^8.1.4",
    "prettier": "^1.18.2",
    "rimraf": "^2.6.3",
    "stylelint": "^10.1.0",
    "stylelint-config-standard": "^18.3.0",
    "stylelint-order": "^3.1.0"
  }
}

当我尝试从根目录写入 yarn 运行 eslint 时出现错误:

$ yarn run eslint yarn run v1.17.3

warning ../package.json: No license field $ eslint config '**/*.js' --ext .js

Oops! Something went wrong! :(

ESLint: 6.4.0.

Failed to read JSON file at

/Users/vladyslavsymonenko/Chess/server/.eslintrc.json:

Cannot read config file: >/Users/vladyslavsymonenko/Chess/server/.eslintrc.json Error: Unexpected token ] in JSON at position 1040

error Command failed with exit code 2. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about >this command.

我对结果真的很困惑,请帮助我)

您需要从 .eslintrc 中删除所有尾随逗号(如第 7 行的 "prettier",),因为它无效 JSON:

{
    "parser": "babel-eslint",
    "extends": [
        "airbnb",
        "plugin:node/recommended",
        "plugin:security/recommended",
        "prettier"
    ],
    "env": {
        "node": true,
        "commonjs": true,
        "es6": true
    },
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "plugins": [
        "prettier"
    ],
    "rules": {
        "prettier/prettier": "error",
        "node/exports-style": ["error", "module.exports"],
        "node/file-extension-in-import": ["error", "always"],
        "node/prefer-global/buffer": ["error", "always"],
        "node/prefer-global/console": ["error", "always"],
        "node/prefer-global/process": ["error", "always"],
        "node/prefer-global/url-search-params": ["error", "always"],
        "node/prefer-global/url": ["error", "always"],
        "node/prefer-promises/dns": "error",
        "node/prefer-promises/fs": "error"
    }
}