Skip to main content
  1. Articles/

Pre-commit hooks for Node.js projects

·
Ro'i Bandel
Author
Ro’i Bandel
Table of Contents

This is my workflow for integrating pre-commit hooks for Node.js (NPM) projects. I combine this with my Oxc Workflow.

What is a Git pre-commit hook?

A pre-commit is a type of Git Hook that runs before each commit. It can help with verifying code standards (linting, formatting, testing etc.).

Pre-commit tools

pre-commit

A tool written in Python, though can be used with projects in any language. Can be configured to run many hooks including pre-commit/pre-commit-hooks and Gitleaks. I have used this tool and like it for Python and other projects, though for Node.js projects, I prefer the options below.

Husky

A pre-commit hooks tool written in JavaScript. I prefer this tool for Node.js projects since it can be easily integrated in package.json scripts.

lint-staged

Another tool that’s written in JavaScript, to help run checks against staged files (see Guide below). Lint-staged does not configure git pre-commit hooks on its own, but can be combined with Husky.

simple-git-hooks

Another git hooks manager written in JavaScript. Use to be more lightweight than Husky, but newer versions of Husky closed the gap.

Guide: Husky + Lint-staged + Oxc workflow

  1. Configure oxlint and oxfmt based on my Oxc Workflow.

  2. Install devDependencies:

npm install --save-dev husky lint-staged
  1. Initialize husky:
# Installed
./node_modules/.bin/husky --init

# Not installed
npx run husky --init
  1. Configure lint-staged to run Oxc and tsc checks:
[/**
 * @filename: lint-staged.config.js
 * @type {import('lint-staged').Configuration}
 */
export default {
  '**/*.[jt]s?(x)': [
    'oxfmt',
    'oxlint --type-aware --type-check --fix',
  ],
  '**/*.ts?(x)': () => 'tsc -p tsconfig.json --noEmit',
}
Note

Can be further configured, but this is a good start for a project using TypeScript and Oxc.

  1. Configure husky to run lint-staged as a pre-commit hook:
npm exec -- lint-staged --config lint-staged.config.js
  1. Add a preparescript in package.json :
  "scripts": {
    "prepare": "husky"
  },
This script may have already been added by husky --init.

Featured image by Yancy Min on Unsplash.

Related

Oxc Workflow

·
How to setup new Node.js projects, with linting and formatting using Oxc.

Next Generation Tooling for Developers

·
In recent months I have been learning about Astral, and have started using uv and ruff. This led me to try to find similar tools for other languages.

Git Setup for Windows and WSL

·
Configure Git once for both Windows and WSL: install Git and GCM, reuse your .gitconfig, and enable seamless credential handling across environments.

 MCP Security Oxc Workflow