Express.js Mastery / Introduction To Node.js And NPM

Setting up Prettier in VS Code

🎯 Purpose of This Note

A quick overview of a recommended VS Code extension setup, followed by a step-by-step guide to installing and configuring Prettier β€” a code formatter that will be used throughout the rest of the course.


🧩 VS Code Extensions Overview

πŸ“¦ Below are the extensions mentioned, with brief explanations of what each does:

🧩 Extension

🎯 Purpose

.env

🌱 Highlights environment variables inside .env files (useful later in the course)

ESLint

πŸ›‘οΈ Detects bugs and enforces code-quality rules in JavaScript (will be set up with Node in a later section)

Image Preview

πŸ–ΌοΈ Shows a small thumbnail preview of image files directly in the file explorer/sidebar

Prettier

✨ Automatically formats code (JavaScript, CSS, HTML, etc.) β€” covered in detail below

Pug Beautify

πŸ“„ Formats Pug template files (Pug is a templating language used later in the course)

TabNine

πŸ€– An AI-powered autocomplete tool that helps speed up coding

TODO Highlight

πŸ“Œ Highlights special comment keywords in different colors for visibility

Oceanic Next (Theme)

🎨 The color theme used throughout the course (mentioned since it's a commonly asked question)

🚦 TODO Highlight in Action

  • 🌈 This extension colors specific keywords written in code comments:

  • βœ… TODO β†’ highlighted in green.

  • 🐞 BUG β†’ highlighted in red.

  • πŸ”§ FIXME β†’ highlighted in another distinct color.

  • πŸ“ These highlighted lines also show up as colored markers in the scrollbar, making it easy to spot important notes across a long file at a glance.


βš™οΈ Setting Up Prettier

πŸ€” What Prettier Does

  • ✨ Automatically formats your code (JavaScript, CSS, HTML, etc.) to a consistent, opinionated style.

  • 🧹 Handles indentation, quote style, line breaks, semicolons, etc. β€” no manual formatting needed.

  • 🀝 Intentionally has few config options β€” a feature, not a limitation, since it keeps formatting consistent and out of debate, especially on teams.

πŸ“₯  Step 1: Install the Prettier Extension

  • πŸ” Search for "Prettier" in the VS Code Extensions panel and click Install.

πŸ’Ύ  Step 2: Enable "Format on Save"

  1. βš™οΈ Open VS Code Settings.

  2. πŸ”Ž Search for: Format on Save.

  3. βœ… Enable the checkbox.

  • πŸ’Ύ Now, every time you save a file, Prettier will automatically reformat it according to its rules.

πŸ“„ Step 3: Create a Prettier Configuration File

  • πŸ“‚ Instead of configuring Prettier through VS Code's settings UI, create a dedicated config file in your project's root folder:

.prettierrc

πŸ€” Why Use a Config File Instead of VS Code Settings?

  • Settings inside a .prettierrc file are project-specific and can travel with the codebase.

  • This makes it easy to:

    • πŸ“ Use different formatting rules across different projects.

    • πŸ‘₯ Ensure all developers on a team use the exact same formatting rules (very useful for collaborative projects).

πŸ“ Example Configuration

{
  "singleQuote": true
}
  • "singleQuote": true tells Prettier to convert all double quotes (") to single quotes (') throughout your code.

  • The default value for singleQuote is false (meaning Prettier normally prefers double quotes unless told otherwise).

πŸ§ͺ Step 4: Testing Prettier in Action

After saving .prettierrc, save any JavaScript file (e.g., index.js) and watch for:

  1. πŸ”€ Quotes converted β€” double quotes automatically switch to single.

  2. 🧹 Extra blank lines collapsed β€” Prettier allows only one blank line between code blocks.

    • ⚠️ Not configurable β€” one of the few restrictive parts of an otherwise well-liked tool.

  3. πŸ“ Long lines wrapped β€” lines past a character limit get intelligently broken up.

  4. πŸ“ Semicolons added back β€” delete them and save, Prettier restores them.

βš™οΈ Step 5: More Prettier Options

Search "Prettier" to see its full option list on the official site (described as an "opinionated code formatter" with intentionally few settings), including:

βš™οΈ Option

🎯 Controls

printWidth

πŸ“ Max line length before wrapping (default: 80)

tabWidth

↔️ Spaces per indentation level (default: 2)

singleQuote

πŸ”€ Single quotes vs. double quotes (default: false)


🎯 Key Takeaways

πŸ“Œ Concept

πŸ’‘ Detail

✨ Prettier

An opinionated code formatter for JS, CSS, HTML, etc.

πŸ’Ύ Format on Save

VS Code setting that triggers Prettier automatically every time you save a file

πŸ“„ .prettierrc

Project-level configuration file for Prettier settings β€” better than VS Code UI settings for consistency across a team

πŸ”€ singleQuote: true

Converts double quotes to single quotes throughout your code

πŸ“ printWidth

Controls the max line length before wrapping (default: 80)

↔️ tabWidth

Controls indentation spacing (default: 2) β€” not the same as printWidth

🚫 Not fully configurable

Prettier intentionally limits customization (e.g., you can't allow multiple blank lines) to keep formatting consistent


πŸŽ‰ Wrap-Up

  • 🎊 This Notes concludes the Node Farm project and the introductory Node.js section.

  • πŸš€ From this point forward in the course, all code will be automatically formatted using this Prettier + .prettierrc (singleQuote: true) setup with Format on Save enabled.