Next.js + Prettier + Husky setup
Cut the crap and start directly Introduction Getting started is intentionally simple. You can create a new project with: npx create-next-app@latest . That's enough to create a Next.js project . But creating the project is only the first step. For a real application, you will usually want consistent code formatting, linting, Git hooks, environment-variable conventions, and a project structure that…
Introduction
Setting up a Next.js project with Prettier and Husky can streamline the development process, ensuring consistent code formatting, linting, and version control. This guide walks through the steps to create a clean development environment for full-stack development.
Prerequisites
Before starting, ensure that Node.js 20.9 or newer is installed. Verify the version by running `node -v`. npm should be installed along with Node.js; verify it using `npm -v`. Git is recommended for version control and will be used by Husky later. Check its version with `git --version`. Visual Studio Code is a good choice for Next.js development, along with recommended extensions such as ESLint, Prettier, Tailwind CSS IntelliSense, Error Lens, and GitLens (optional).
Creating the Next.js Application
To create a new Next.js project, run `npx create-next-app@latest` in the current directory. The Next.js CLI will prompt you with several questions. For a modern full-stack application, select TypeScript as the language, ESLint as the linter, Tailwind CSS as the styling framework, and enable the App Router. The CLI also supports an --import-alias option and can initialize a project inside the src/ directory.
After completing the setup, start the development server by running `npm run dev` and open http://localhost:3000 to verify the application has been successfully created.
Initialization and Configuration
Initialize Git by running `git init` and check the repository status with `git status`. Create the initial commit with `git add .` and `git commit -m "chore: initialize Next.js project"`. From this point onward, Git will track your project changes.
Install and configure Prettier, a tool that automatically formats your code according to a consistent style. Install it locally using `npm install --save-dev --save-exact prettier`. Create a `.prettierrc` file with the desired formatting options, such as `semi`, `singleQuote`, `trailingComma`, `tabWidth`, and `printWidth`. Create a `.prettierignore` file to specify files and directories to ignore during formatting, such as `node_modules`, `.next`, `out`, `dist`, `coverage`, `public`, and any lock files.
Finally, format the project by running `npx prettier . --write` to ensure all files are formatted consistently. To check the formatting without modifying files, use `npx prettier . --check`.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.