Skip to content
Santi020k

Search

Match titles, tags, and descriptions. Arrow keys to move, Enter to open, Esc to close.

Open from the page (not while typing in a field): / · K or CtrlK

    Permalink to this article
    Blog Article
    pre-commit

    Development Workflow with Husky for Next.js, ESLint, and Vitest Integration

    A practical Husky setup for Next.js projects so linting, testing, and pre-push checks become part of the team workflow instead of a last-minute cleanup step.

    2 min read

    372

    Read the Previous Post: Building the Best Next.js TypeScript ESLint Configuration

    GitHub Repository

    Implementing a robust pre-commit system can significantly reduce pull request review time. By enforcing ESLint rules, ensuring passing tests, and maintaining a stable build, developers can avoid pushing broken builds and the subsequent need for corrective commits.

    This approach improves code quality while reducing CI/CD expenses.

    Setting Up Pre-commit and Pre-push

    Start by installing the required dependencies:

    terminal
    npm install --save-dev husky lint-staged
    # or
    yarn add husky lint-staged --dev

    To organize Husky within a config folder, modify your package.json:

    package.json
    {
      "scripts": {
        "dev": "next dev",
        "build": "next build",
        "start": "next start",
        "lint": "next lint",
        "test": "vitest",
        "pre-commit": "lint-staged && vitest run",
        "pre-push": "eslint . --fix --max-warnings=0 && npm run build",
        "prepare": "husky install config/.husky"
      },
      "lint-staged": {
        "*.{js,jsx,ts,tsx}": [
          "eslint --fix"
        ]
      }
    }

    This establishes commands for pre-commit and pre-push operations. Customize according to your project requirements.

    Description of Actions

    The pre-commit script executes linting with fixes and runs tests before allowing commits:

    package.json
    "pre-commit": "lint-staged && vitest run"

    The pre-push script enforces strict linting standards and triggers the build process:

    package.json
    "pre-push": "eslint . --fix --max-warnings=0 && npm run build"

    Execute the prepare command to generate the hook files:

    terminal
    npm run prepare
    # or
    yarn run prepare

    This generates the necessary files in the config/.husky directory. These files allow Husky to execute the specified commands whenever a commit or push is made.

    If you ever need to bypass Husky’s verification — for example during a work in progress — you can use --no-verify:

    terminal
    git commit . -m 'quick fix' --no-verify

    Bypassing verification risks overlooking console errors and is not recommended as a regular practice.

    Conclusions

    • Husky pre-commit and pre-push hooks improve code stability through enforced linting and testing protocols.
    • Organizing configurations within a dedicated config folder maintains project cleanliness and simplifies maintenance.
    • Early error detection reduces corrective commits and workflow disruptions.
    • While bypass options exist, circumventing verification processes compromises code reliability.
    • Integrating Husky fosters quality-focused development practices and a culture of continuous improvement.

    Next Post: Storybook in Action with Next.js, Tailwind and TypeScript

    Share this piece
    Keep reading

    More writing in the same thread.

    A few more posts that overlap in topic, tooling, or the engineering tradeoffs behind this article.

    Boosting Code Quality and Efficiency with My ESLint Configuration Library
    Writing Case Study 5 min read

    Boosting Code Quality and Efficiency with My ESLint Configuration Library

    Reusable ESLint library for React, Next.js, and TypeScript projects. Enforces code quality with flat config support and simplifies the move from ESLint 8 to 9.

    In series: ESLint in Practice · Part 2

    Get new posts in your inbox

    Low volume — engineering notes, architecture writeups, and occasional career updates. Prefer a feed? Subscribe via RSS .

    We never share your email. Privacy policy .