Dev Encyclopedia
ArticlesTools
ArticlesTools

Get notified when new content drops

No spam. Just new articles, tools, and updates — straight to your inbox.

Dev Encyclopedia

A reference for builders

Content

  • Articles
  • Tools
  • Contact

Connect

  • support@devencyclopedia.com
  • RSS Feed

© 2026 Dev Encyclopedia

Privacy PolicyTermsDisclaimer
  1. Home
  2. /Blog
  3. /Husky + Prettier + lint-staged Setup for Next.js
nextjs8 min read

Husky + Prettier + lint-staged Setup for Next.js

Set up Husky v9, Prettier, and lint-staged in your Next.js project. Step-by-step guide covering pre-commit hooks with the correct 2026 config.

By Dev EncyclopediaPublished May 30, 2026
On this page

On this page

  • Introduction
  • What These Tools Do
  • Install the Tools
  • Configure Prettier
  • Initialize Husky v9
  • Configure the Pre-Commit Hook
  • Configure lint-staged
  • Test the Setup
  • Common Issues and Fixes
  • Frequently Asked Questions

Introduction

Most code quality issues are caught too late — in code review, or worse, in production. Husky, Prettier, and lint-staged give you an automatic check on every single commit, right on your machine, before the code ever leaves your editor.

This guide walks through the full setup for a Next.js project using the current versions of all three tools. If you've tried this before with a guide from 2022 and it didn't work, it's probably because Husky changed its configuration format in v9.

Step-by-Step Guide

1

What These Tools Do

Three tools, each with a distinct job. Together they form an automatic quality gate that runs on every commit:

  • Prettier formats your code automatically. You set the rules once — line length, single vs double quotes, trailing commas — and it handles the rest.
  • lint-staged runs linters only on the files you've staged for a commit, not your entire codebase. This keeps the pre-commit check fast even on large projects.
  • Husky connects everything to Git. It installs scripts that run automatically at key moments — in this setup, immediately before a commit is finalized.
2

Install the Tools

From your Next.js project root, install the three dev dependencies. Next.js already ships with ESLint configured, so you don't need to install that separately.

bash
npm install --save-dev prettier husky lint-staged
3

Configure Prettier

Create a .prettierrc file in your project root. These are sensible defaults for a Next.js TypeScript project — adjust them to your team's preference:

json
{
  "semi": true,
  "singleQuote": true,
  "trailingComma": "es5",
  "printWidth": 100,
  "tabWidth": 2
}

💡 Tip

Also create a .prettierignore file with node_modules, .next, out, and public. This stops Prettier from touching generated files and the build output.

4

Initialize Husky v9

This is where most guides go wrong. Husky v9 dropped the old .huskyrc file format. Run one command to initialize it:

bash
npx husky init

ℹ Info

This creates a .husky/ directory and adds "prepare": "husky" to package.json. Any developer who runs npm install will automatically have Husky configured — no manual setup needed.

5

Configure the Pre-Commit Hook

Open .husky/pre-commit — it was just created by husky init. Replace its contents with a single line that triggers lint-staged:

sh
npx lint-staged

⚠ Warning

Don't add prettier --write . or eslint . directly here — that runs on your entire codebase every commit. lint-staged is what scopes the run to only your staged files.

6

Configure lint-staged

Add a lint-staged key to your package.json. This tells it which tools to run on which file types when they're staged:

json
{
  "lint-staged": {
    "*.{js,jsx,ts,tsx}": [
      "eslint --fix",
      "prettier --write"
    ],
    "*.{json,css,md}": [
      "prettier --write"
    ]
  }
}

💡 Tip

The --fix and --write flags tell both tools to auto-fix what they can before committing. Only issues ESLint can't auto-fix will block the commit.

7

Test the Setup

Stage a file with a formatting issue and try to commit. lint-staged will run both tools automatically:

  • If Prettier can fix the issue automatically, it will do so and the commit will succeed.
  • If ESLint flags something it can't auto-fix (like an unused variable), it will print the error and block the commit.
  • Try it both ways — this is the fastest way to understand exactly what the setup catches.
bash
git add .
git commit -m "test commit"
8

Common Issues and Fixes

These are the four most common problems developers hit after setting this up:

  • Hooks not running after cloning. Someone ran npm install --ignore-scripts, which skips the prepare script. Fix: run npm run prepare manually once.
  • `husky: command not found`. An old .huskyrc or husky.config.js from a previous attempt is still present. Delete it and re-run npx husky init.
  • `lint-staged` runs on all files, not just staged ones. The .husky/pre-commit file contains prettier --write . instead of npx lint-staged. Replace it with npx lint-staged.
  • TypeScript errors not being caught. This is expected — lint-staged runs tools on individual files in isolation. TypeScript's type checker needs the full project to work. Add tsc --noEmit to a pre-push hook instead.

ℹ Info

The TypeScript pre-push approach: create .husky/pre-push and add npx tsc --noEmit. This runs the full type check before a push, not before every commit — the right tradeoff for build speed vs. safety. Also set HUSKY=0 in your CI environment to skip hooks during automated builds.

Frequently Asked Questions

What changed between Husky v8 and v9?
Husky v9 simplified the setup significantly. The husky install command is gone, replaced by husky init. The .huskyrc file format is no longer supported. Hooks are now plain shell scripts in the .husky/ directory, and the prepare lifecycle script handles automatic installation.
Why use lint-staged instead of running ESLint on the whole project?
Running ESLint on a large codebase before every commit gets slow fast — 10, 20, 30 seconds per commit. lint-staged scopes the run to only the files you've changed, keeping pre-commit checks under a second on most projects.
Should I use Prettier, ESLint, or both?
Both, for different reasons. ESLint catches code quality issues — unused variables, unreachable code, type errors when configured with TypeScript rules. Prettier handles formatting — indentation, line breaks, quote style. They don't overlap. Run ESLint first, then Prettier, so Prettier's final formatting pass is what gets committed.
Will Husky hooks run in CI?
By default, yes — but you usually don't want them to. In CI, you want to run the full lint and type-check directly, not via Husky. Set the HUSKY environment variable to 0 in your CI config to skip Husky hooks: HUSKY=0 npm ci.
Does this setup work with Yarn or pnpm?
Yes. Replace npm install --save-dev with yarn add --dev or pnpm add -D. The Husky init and lint-staged configuration are identical — only the package manager command changes.

Husky v9, Prettier, and lint-staged take about five minutes to set up and save hours of back-and-forth over formatting in code review.

Keep the pre-commit hook fast — format and lint only staged files. Move tsc --noEmit to a pre-push hook where a slower check is acceptable.

Related Articles

nextjs

How to Use Environment Variables in Next.js (Without Leaking Them to the Browser)

Learn how to use .env files in Next.js correctly. Understand NEXT_PUBLIC_, avoid common mistakes, and set variables in Vercel and Cloudflare.

May 30, 2026·7 min read
typescript

TypeScript 7 (Project Corsa): What Next.js Devs Need to Know

TypeScript 7 rewrites the compiler in Go for 10x faster builds. Here's what it means for your Next.js project and what to do right now.

May 30, 2026·7 min read

On this page

  • Introduction
  • What These Tools Do
  • Install the Tools
  • Configure Prettier
  • Initialize Husky v9
  • Configure the Pre-Commit Hook
  • Configure lint-staged
  • Test the Setup
  • Common Issues and Fixes
  • Frequently Asked Questions