如何使用 nodejs express 应用程序配置 eslint

新手上路,请多包涵

应用程序。我需要为此应用程序使用 eslint。我正在使用 https://www.npmjs.com/package/eslint-config-airbnb 并在 VS 代码编辑器中使用更漂亮的插件。

.eslintrc

 {
  "extends": "airbnb"
}

我看到在添加 eslint 插件 https://marketplace.visualstudio.comitems?itemName=dbaeumer.vscode-eslint 和 npm 包后,VS Code 现在在整个项目中给我很多错误。

很少有错误

[eslint] Definition for rule 'jsx-a11y/href-no-hash' was not found (jsx-a11y/href-no-hash)
[eslint] Expected linebreaks to be 'LF' but found 'CRLF'. (linebreak-style)
[eslint] Unexpected unnamed function. (func-names)
[eslint] Missing space before function parentheses. (space-before-function-paren)
[eslint] Strings must use singlequote. (quotes)
[eslint] Unexpected function expression. (prefer-arrow-callback)
[eslint] Unexpected unnamed function 'bind'. (func-names)
[eslint] Missing space before function parentheses. (space-before-function-paren)

包.json

   "devDependencies": {
    "babel": "^6.23.0",
    "babel-cli": "^6.5.1",
    "babel-core": "^6.9.0",
    "babel-eslint": "^7.2.3",
    "babel-preset-es2015": "^6.5.0",
    "babel-preset-stage-0": "6.5.0",
    "eslint": "^3.19.0",
    "eslint-config-airbnb": "^15.1.0",
    "eslint-plugin-import": "^2.7.0",
    "eslint-plugin-jsx-a11y": "^6.0.2",
    "eslint-plugin-react": "^7.4.0",
    "nodemon": "^1.12.1"
  }

索引.js

 import request from "superagent";

module.exports = function(req, res, next) {
  const id = "abc";

  request
    .post(url)
    .send(`p1=v1`)
    .send(`p2=v2`)
    .end(function(error, response) {}.bind(this));
  next();
};

在此处输入图像描述

每个 JS 文件中的错误类型相同。有谁知道如何解决这些问题?

原文由 N Sharma 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 698
1 个回答
  1. 将以下模块添加到您的 devDependencies 使用:
 npm install --save-dev eslint
npm install --save-dev eslint-config-airbnb-base
npm install --save-dev eslint-plugin-import

  1. eslintConfig 部分添加到您的 package.json
 "eslintConfig": {
    "extends": "airbnb-base",
    "env": {
        "es6": true,
        "browser": true
    },
    "rules": {
        "brace-style": [
            "error",
            "stroustrup"
        ],
        "comma-dangle": [
            "error",
            "never"
        ],
        "no-unused-vars": [
            "warn"
        ],
        "no-var": [
            "off"
        ],
        "one-var": [
            "off"
        ]
    }
}

  1. 访问 eslint.org/docs/rules ,搜索您想要调整的警告并将它们添加到上面的 eslintConfig 中。

  2. 删除项目根目录中的 .eslintrc 文件。

5)重新启动你的IDE

原文由 John Doherty 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题