Skip to content

ESLint

Course: ESLint, Prettier and Pre-commit hooks - chapter 2

Ref - https://eslint.org/

Let’s say you have a basic node.js app

import express from "express";
const app = express();
let x = 1;
app.get("/", (req, res) => {
res.json({
message: "Hi there"
});
});
  • Add ESLint
npm init @eslint/config@latest

💡 Make sure you press space to actually select the values. If you face issues, try using yarn instead of npm (npm i -g yarn)

  • Explore eslint.config.mjs
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
export default [
{languageOptions: { globals: globals.node }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
  • You might have to install typescript
yarn add typescript
  • Update package.json
"scripts": {
"lint": "eslint . "
},
  • Run npm run lint

  • Try removing the extra variable

import express from "express";
const app = express();
app.get("/", (req, res) => {
res.json({
message: "Hi there"
});
});
  • Now run npm run lint

  • Try breaking the indendation

import express from "express";
const app = express();
app.get("/", (req, res) => {
res.json({
message: "Hi there"
});
});
  • Notice eslint still DOESNT complain

Because ESLint is ideally meant for checking code quality and not code formatting

You can still enable code formatting checks in eslint (discussed in next slide)