Set up pre-commit hooks with Husky, lint-staged, Prettier

VerifiedSafe

Set up Husky, lint-staged, and Prettier to add a pre-commit hook that formats files, type-checks, and runs tests.

Sby Skills Guide Bot
DevelopmentBeginner
8/29/2026
Claude CodeCursorWindsurfCopilotCodex
#pre-commit-hooks#husky#lint-staged#prettier#code-quality

Recommended for

Automated summary

This skill wires up Husky pre-commit hooks in the current repo that run lint-staged with Prettier, followed by typecheck and tests before each commit.

Strengths

  • Auto-detects the package manager (npm, pnpm, yarn, bun) from the lockfile present in the repo
  • Runs fast staged-file formatting first, then full typecheck and test suites in the hook
  • Only writes a default Prettier config when none already exists
  • Finishes with a commit that doubles as a smoke test of the new hooks

Limitations

  • Scoped to JavaScript/TypeScript projects and Husky v9+
  • Silently drops the typecheck and test lines when the matching package.json scripts are missing (it does report this to the user)
  • Runs whole-project typecheck and tests on every commit, which can be slow on large repositories
When to use it

Use it when a JS/TS repo has no commit hooks and you want formatting, type checking and tests enforced at commit time.

When not to use it

Avoid it on non-JavaScript projects, on repos already using another hook manager (Lefthook, Python pre-commit), or when full checks on every commit are unwanted.

Security analysis

Safe
Quality score90/100

The skill installs common developer dependencies (husky, lint-staged, prettier), initializes Husky, and creates pre-commit hook configuration. It does not contain destructive commands, exfiltration, obfuscation, or disabling of safety features. The commands are standard package-manager and formatting tool invocations for a legitimate developer workflow.

No concerns found

How this analysis is made

Examples

Add hooks to existing repo
Set up Husky pre-commit hooks in this repo with lint-staged and Prettier, plus typecheck and tests before each commit.
Configure lint-staged
Configure lint-staged to run Prettier on all staged files and hook it into a Husky pre-commit hook.
Enforce formatting on commit
I want formatting, type checking and tests to run automatically whenever I commit — please wire that up.

name: setup-pre-commit description: Set up Husky pre-commit hooks with lint-staged (Prettier), type checking, and tests in the current repo. Use when user wants to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting/typechecking/testing.

Setup Pre-Commit Hooks

What This Sets Up

  • Husky pre-commit hook
  • lint-staged running Prettier on all staged files
  • Prettier config (if missing)
  • typecheck and test scripts in the pre-commit hook

Steps

1. Detect package manager

Check for package-lock.json (npm), pnpm-lock.yaml (pnpm), yarn.lock (yarn), bun.lockb (bun). Use whichever is present. Default to npm if unclear.

2. Install dependencies

Install as devDependencies:

husky lint-staged prettier

3. Initialize Husky

npx husky init

This creates .husky/ dir and adds prepare: "husky" to package.json.

4. Create .husky/pre-commit

Write this file (no shebang needed for Husky v9+):

npx lint-staged
npm run typecheck
npm run test

Adapt: Replace npm with detected package manager. If repo has no typecheck or test script in package.json, omit those lines and tell the user.

5. Create .lintstagedrc

{
  "*": "prettier --ignore-unknown --write"
}

6. Create .prettierrc (if missing)

Only create if no Prettier config exists. Use these defaults:

{
  "useTabs": false,
  "tabWidth": 2,
  "printWidth": 80,
  "singleQuote": false,
  "trailingComma": "es5",
  "semi": true,
  "arrowParens": "always"
}

7. Verify

  • [ ] .husky/pre-commit exists and is executable
  • [ ] .lintstagedrc exists
  • [ ] prepare script in package.json is "husky"
  • [ ] prettier config exists
  • [ ] Run npx lint-staged to verify it works

8. Commit

Stage all changed/created files and commit with message: Add pre-commit hooks (husky + lint-staged + prettier)

This will run through the new pre-commit hooks: a good smoke test that everything works.

Notes

  • Husky v9+ doesn't need shebangs in hook files
  • prettier --ignore-unknown skips files Prettier can't parse (images, etc.)
  • The pre-commit runs lint-staged first (fast, staged-only), then full typecheck and tests

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr/download' --output '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr/SKILL.md.part' && mv '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr/SKILL.md.part' '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

No recognized prerequisites section in this file. This does not mean it is self-contained: libraries, tools and access requirements may be mentioned elsewhere in the content or source repository.

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr/SKILL.md' && test -s '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/setup-pre-commit' && cp -R '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr' '.claude/skills/setup-pre-commit'

Cursor
test -f '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr/SKILL.md' && test -s '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/setup-pre-commit' && cp -R '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr' '.cursor/skills/setup-pre-commit'

Codex
test -f '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr/SKILL.md' && test -s '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr/SKILL.md' && mkdir -p '.agents/skills' && test ! -e '.agents/skills/setup-pre-commit' && cp -R '.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr' '.agents/skills/setup-pre-commit'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr'

openskills read 'set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/set-up-pre-commit-hooks-with-husky-lint-staged-prettier-mte1lkhr/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills