简体   繁体   English

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

[英]How to configure eslint with nodejs express application

js application. js 应用程序。 I need to use eslint for this application.我需要为此应用程序使用 eslint。 I am using https://www.npmjs.com/package/eslint-config-airbnb and using prettier plugin in VS Code editor.我正在使用https://www.npmjs.com/package/eslint-config-airbnb并在 VS Code 编辑器中使用更漂亮的插件。

.eslintrc .eslintrc

{
  "extends": "airbnb"
}

I see that VS Code is giving me lot of errors in complete project now after adding eslint plugin https://marketplace.visualstudio.comitems?itemName=dbaeumer.vscode-eslint and npm package.我看到在添加 eslint 插件https://marketplace.visualstudio.comitems?itemName=dbaeumer.vscode-eslint和 npm 包后,VS Code 现在在完整项目中给了我很多错误。

Few errors错误少

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

package.json包.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"
  }

index.js索引.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();
};

在此处输入图片说明

Same kind of errors in each JS files.每个 JS 文件中都有相同类型的错误。 Does anyone know how to resolve these ?有谁知道如何解决这些问题?

1) Add the following modules to your devDependencies using: 1) 使用以下方法将以下模块添加到您的devDependencies中:

npm install --save-dev eslint
npm install --save-dev eslint-config-airbnb-base
npm install --save-dev eslint-plugin-import

2) Add an eslintConfig section to your package.json : 2) 将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"
        ]
    }
}

3) Visit eslint.org/docs/rules , search for the warnings you want to tweak and add them to the eslintConfig above. 3) 访问eslint.org/docs/rules ,搜索您要调整的警告并将它们添加到上面的eslintConfig

4) Delete the .eslintrc file in the root of your project. 4) 删除项目根目录下的.eslintrc文件。

5) Restart your IDE 5)重启你的IDE

Install eslint package locally to your project.将 eslint 包本地安装到您的项目中。

$yarn add eslint --dev

Ideally one can generate configuration file .eslintrc through below command, unfortunately it installs wrong versions of peerDependencies resulting in weired linting errors.理想情况下,可以通过以下命令生成配置文件 .eslintrc,不幸的是,它安装了错误版本的 peerDependencies,从而导致了奇怪的 linting 错误。

$./node_modules/.bin/eslint --init

Easier way to fix the version of peerDependencies issue, use below command and install(I suggest you to install packages related to jsx and react though you may not doing any stuff related to React) corresponding version of the peerDependencies along with eslint-config-airbnb更容易修复 peerDependencies 版本问题的方法,使用下面的命令并安装(我建议你安装与 jsx 相关的包并做出反应,尽管你可能不会做任何与 React 相关的事情)对应版本的 peerDependencies 以及 eslint-config-airbnb

$npm info "eslint-config-airbnb@latest" peerDependencies

Edit .eslintrc file with below content使用以下内容编辑 .eslintrc 文件

{
  "extends": "airbnb",
  "plugins": [
    "react",
    "jsx-a11y",
    "import"
  ],
  "rules": {
  },
  "env": {
  }
}

*Note: Please edit eslintrc file depending on your coding rules, environment you follow. *注意:请根据您的编码规则,您遵循的环境编辑eslintrc文件。 Refer developer-guide请参阅开发人员指南

ESLint wrote messages what you need to do. ESLint 写了你需要做的消息。 Just read it.只需阅读它。

Correct code with this rules should be following:具有此规则的正确代码应如下所示:

import request from 'superagent';

module.exports = (req, res, next) => {
  const id = 'abc';

  request
    .post(url)
    .send('p1=v1')
    .send('p2=v2')
    .end((error, response) => {
      // do something on end
    });
  next();
};

Other answers are close, hope this answer will help in a complete answer:其他答案很接近,希望这个答案有助于完整的答案:

1: 1:

File .eslintrc.js文件.eslintrc.js

module.exports = {
  env: {
    es6: true,
    node: true,
  },
  extends: "eslint:recommended",
  globals: {
    Atomics: "readonly",
    SharedArrayBuffer: "readonly",
  },
  // parser: "babel-eslint",
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: "module",
  },
  // plugins: [],
  rules: {
    "no-console": "off", // "warn" // "off"
  },
  settings: {},
};

2: 2:

File .eslintignore文件.eslintignore

node_modules/

3: 3:

File package.json文件package.json

{
  "scripts": {
    "lint": "eslint \"./**/*.js\" --quiet",
    "lintFull": "eslint \"./**/*.js\"",
    "lintFix": "eslint --fix './**/*.js'"
  },
  "devDependencies": {
    "eslint": "^6.8.0",
  }
}

4: 4:

npm i
npm run lint

That's all仅此而已

---------------- ----------------

But, additionally, if you want to make your own .eslintrc.js with custom settings like Typescript, etc. try the below:但是,此外,如果您想使用自定义设置(如 Typescript 等)制作自己的 .eslintrc.js,请尝试以下操作:

1: 1:

npm i eslint -g

2: 2:

eslint --init

? How would you like to use ESLint? To check syntax and find problems
? What type of modules does your project use? JavaScript modules (import/export)
? Which framework does your project use? None of these
? Does your project use TypeScript? No
? Where does your code run? Node
? What format do you want your config file to be in? JavaScript
    Successfully created .eslintrc.js file in C:\repo\nodeapp1

Hope that helps.希望有帮助。

If you are using atom, vs code... maybe the editor needs to install globally eslint.如果你使用 atom,vs 代码...也许编辑器需要全局安装 eslint。

npm install -g eslint npm install -g eslint

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

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