Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure eslint with nodejs express application

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

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

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

  "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

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 ?

like image 615
N Sharma Avatar asked Oct 04 '17 10:10

N Sharma


People also ask

How do I use ESLint in node project?

To configure ESLint on your Node. js project, you will need to install ESLint globally on your computer system using the command prompt shown below: Installing ESLint globally on your computer system makes it possible to run ESLint init which will do all the setup for us.


1 Answers

1) Add the following modules to your devDependencies using:

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:

"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.

4) Delete the .eslintrc file in the root of your project.

5) Restart your IDE

like image 156
John Doherty Avatar answered Sep 25 '22 01:09

John Doherty