Express.js Mastery / Express: Let’s Start Building The Natours API!

Setting up ESLint + Prettier in VS Code

πŸ“– Context

Before moving to MongoDB, this lecture covers setting up ESLint together with Prettier to improve code quality. This is optional if not using VS Code or if another linter is already part of the workflow β€” otherwise, it's a worthwhile step.

πŸ’‘ What ESLint does

  • πŸ” Constantly scans code to find potential errors or bad coding practices.

  • βš™οΈ It's highly configurable to match personal coding habits and standards.

🀝 How ESLint and Prettier Work Together

  • 🎨 Prettier remains the main code formatter (already set up in an earlier section).

  • 🚨 ESLint will be configured to only highlight errors/bad practices, based on a defined set of rules β€” not to handle formatting itself.


πŸ”Ή Step 1: Install the ESLint Extension in VS Code

  1. 🧩 Go to the Extensions panel.

  2. πŸ” Search for ESLint and install it.

  3. πŸ”„ Reload the window if prompted.

  4. βœ… Confirm the Prettier extension is also installed


πŸ”Ή Step 2: Install ESLint-Related Dev Dependencies

Install all required packages with one command:

npm install eslint prettier eslint-config-prettier eslint-plugin-prettier eslint-config-airbnb eslint-plugin-node eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react --save-dev

πŸ“¦ What each package does

πŸ“¦ Package

🎯 Purpose

eslint

πŸ› οΈ The core linter

prettier

🎨 The code formatter

eslint-config-prettier

🚫 Disables ESLint's own formatting rules, since Prettier handles formatting

eslint-plugin-prettier

⚠️ Lets ESLint show Prettier formatting issues as actual lint errors while typing

eslint-config-airbnb

πŸ“ A popular, widely-used JavaScript style guide to base the rules on

eslint-plugin-node

🟒 Adds rules specific to catching common Node.js mistakes

eslint-plugin-import, eslint-plugin-jsx-a11y, eslint-plugin-react

πŸ“š Required dependencies of the Airbnb style guide, even though this isn't a React project

πŸ’‘ Important Notes

  • πŸ§ͺ -save-dev β€” these are all dev dependencies, since they're only needed during development, not in production.

  • πŸ“ All packages must be installed locally to the project β€” installing them globally would not work correctly.

  • 🍳 This entire setup is described as a "recipe" to follow rather than something requiring deep understanding β€” the same package.json dependency list can simply be copied into future projects and reinstalled.


πŸ”Ή Step 3: Configuration Files

Two configuration files are needed β€” both already provided as starter files in this project.

πŸ“„ .prettierrc

Defines Prettier's formatting preferences (e.g. always using single quotes).

πŸ“„ .eslintrc.json

{
  "extends": ["airbnb", "prettier", "plugin:node/recommended"]
}

πŸ’‘ How this works

  • πŸ“ Uses the Airbnb JavaScript style guide.

  • 🎨 Enables Prettier compatibility.

  • 🟒 Adds the Node.js recommended ESLint rules.


πŸ”Ή Step 4: Customizing Individual ESLint Rules

ESLint enforces many rules by default, but each can be individually turned off, set to warn, or left as an error.

πŸ“ Examples of Customized Rules

  • ⚠️ no-console β†’ set to warn so console.log() is allowed during development.

  • 🚫 consistent-return β†’ disabled since functions like Express middleware don't always return a value.

  • ❌ no-unused-vars β†’ kept as an error, with exceptions for common Express parameters (req, res, next, etc.).

πŸ“š Where to Explore More Rules

  • 🌐 The official ESLint documentation lists every configurable rule.

  • πŸ’‘ Rather than reading everything upfront, it's usually easier to adjust rules as they come up during development.


πŸ”Ή Step 5: Seeing ESLint in Action

A few intentional issues are introduced into server.js to demonstrate live linting.

πŸ§ͺ Example 1 β€” Missing Blank Line

  • πŸ“„ Missing a blank line after the require statements.

  • 🚨 ESLint flags it until the blank line is added.

πŸ§ͺ Example 2 β€” Reassigning a const

const x = 23;
x = 66;
  • ❌ ESLint reports an error because constants cannot be reassigned.

Now remove this:

const x = 23;
x = 66;

πŸ”Ή Step 6: Reusing This Setup in Future Projects

To use the same setup again:

  1. πŸ“‹ Copy the ESLint-related dependencies from package.json.

  2. πŸ“¦ Run the install command with -save-dev.

πŸ“Œ Installing a Specific Version

npm install eslint@5.16.0

βœ… Key Takeaways

  • πŸ› οΈ ESLint finds code issues, while 🎨 Prettier handles formatting.

  • πŸ“ Airbnb provides a popular base ESLint configuration.

  • βš™οΈ ESLint rules can be configured as off, warn, or error.

  • 🚨 no-unused-vars catches unused variables, with exceptions for Express parameters like req, res, and next.

  • πŸ“‹ This ESLint/Prettier setup can be reused across future Node.js projects.


➑️ Note

You can also see all the eslint related issues in the problems tab

πŸƒ Moving on to MongoDB β€” one of the biggest and most important sections of the course.