GithubHelp home page GithubHelp logo

setup-eslint-prettier-for-node's Introduction

Setup Eslint and Prettier for Node Project in VSCode

Table of Contents:

Editor Setup

You can use any plugin as you wish to. Here, I'm using VSCode editor.

Plugins

You need to install the following plugins:

  • ESLint - Microsoft
  • Prettier - Code formatter - Prettier
  • Learn with Sumit (Peace of the eye - Dracula version) - Sumit Saha (Optional)

Settings

Follow the steps to create settings.json file inside the project root:

  1. Create a folder/directory named .vscode inside the project root.
  2. Create a file named settings.json inside the .vscode folder/directory.
  3. Copy and paste the below json object into the settings.json file and save it.
{
  // Theme
  "workbench.colorTheme": "Learn with Sumit Theme",

  // config related to code formatting
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnSave": true,
  "[javascript]": {
    "editor.formatOnSave": false,
    "editor.defaultFormatter": null
  },
  "javascript.validate.enable": false, //disable all built-in syntax checking
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true,
    "source.fixAll.tslint": true,
    "source.organizeImports": true
  },
  "eslint.alwaysShowStatus": true
}

If you follow all the previous steps correctly, your vscode should look like this.

Set Line Breaks

Make sure in your VS Code Editor, "LF" is selected as line feed instead of CRLF (Carriage return and line feed). To do that, just click LF/CRLF in bottom right corner of editor, click it and change it to "LF". If you don't do that, you may have face errors.

Linting Setup

In order to lint and format Node project automatically according to most popular airbnb style guide, it is recommended to follow the instructions below:

Dependencies

Install the following dev dependencies one by one.

npm i -D prettier eslint-config-prettier eslint-plugin-prettier

Or, if you are using yarn as your default package manager, try this:

yarn add -D prettier eslint-config-prettier eslint-plugin-prettier

Or, if you are using pnpm as your default package manager, try this:

pnpm add -D prettier eslint-config-prettier eslint-plugin-prettier

To follow airbnb style guide run the following command:

npx install-peerdeps --dev eslint-config-airbnb-base

Or, if you are using pnpm as you default package manager, you can try this:

pnpm dlx install-peerdeps -D eslint-config-airbnb-base

Configure ESLint and Prettier

Now its time to configure the ESLint and Prettier plugins to work together poperly. Create a linting config file manually in the project root folder/directory named .eslintrc.json

Copy and paste the followings into the .eslintrc.json file.

{
  "env": {
    "browser": true,
    "commonjs": true,
    "es2021": true
  },
  "extends": ["eslint:recommended", "prettier", "airbnb-base"],
  "parserOptions": {
    "ecmaVersion": "latest"
  },
  "rules": {
    "no-console": 0,
    "indent": 0,
    "linebreak-style": 0,
    "prettier/prettier": [
      "error",
      {
        "trailingComma": "es5",
        "singleQuote": true,
        "printWidth": 100,
        "tabWidth": 4,
        "semi": true
      }
    ]
  },
  "plugins": ["prettier"]
}

Contact

Amin Babu - [email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.