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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46562730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:46:34  来源:igfitidea点击:

How to configure eslint with nodejs express application

javascriptnode.jseslinteslint-config-airbnbprettier

提问by N Sharma

js application. I need to use eslint for this application. I am using https://www.npmjs.com/package/eslint-config-airbnband using prettier plugin in VS Code editor.

js 应用程序。我需要为此应用程序使用 eslint。我正在使用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-eslintand 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();
};

enter image description here

在此处输入图片说明

Same kind of errors in each JS files. Does anyone know how to resolve these ?

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

回答by John Doherty

1) Add the following modules to your devDependenciesusing:

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 eslintConfigsection 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 eslintConfigabove.

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

4) Delete the .eslintrcfile in the root of your project.

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

5) Restart your IDE

5)重启你的IDE

回答by Gurucharan M K

Install eslint package locallyto 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. Refer developer-guide

*注意:请根据您的编码规则,您遵循的环境编辑eslintrc文件。请参阅开发人员指南

回答by Roman Valihura

ESLint wrote messages what you need to do. Just read it.

ESLint 写了你需要做的消息。只需阅读它。

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

回答by Manohar Reddy Poreddy

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.

希望有帮助。

回答by Pako Navarro

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