Streamline Frontend Development with Husky, lint‑staged, ESLint, and Prettier
This guide walks through automating frontend development tasks—using Husky for Git hooks, lint‑staged for staged file filtering, commitlint for standardized commit messages, ESLint and @typescript-eslint for code checking, and Prettier for formatting—providing installation commands, configuration snippets, and practical usage tips for React + TypeScript projects.
Introduction
Developers often struggle with inconsistent code style, syntax errors, unclear commit messages, and chaotic monorepo commits, which can be alleviated by automating checks and formatting.
Background
Frontend engineering has moved beyond plain JavaScript and jQuery to modular, engineered workflows. This article records a personal exploration of basic frontend engineering practices using React and TypeScript.
Git Hooks with Husky
Husky runs commands in Git hooks, enabling automatic linting, testing, or building before commits or pushes.
Installation:
<code># npm
npx husky-init && npm install
# yarn
yarn dlx husky-init --yarn2 && yarn
# pnpm
pnpm dlx husky-init && pnpm install</code>File Filtering with lint‑staged
lint‑staged runs specified commands only on staged files, ensuring code quality before committing.
Installation:
<code># npm
npm install lint-staged --save-dev
# yarn
yarn add lint-staged --dev
# pnpm
pnpm add lint-staged --save-dev</code>Example
package.jsonconfiguration:
<code>{
"scripts": {
"lint": "eslint src"
},
"lint-staged": {
"src/**/*.{ts,tsx}": [
"npm run lint",
"git add"
]
}
}</code>Pre‑commit hook script:
<code>#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx lint-staged</code>Commit Message Standards with commitlint
commitlint enforces a unified commit message format.
Installation:
<code># npm
npm install @commitlint/cli @commitlint/config-conventional --save-dev
# yarn
yarn add @commitlint/cli @commitlint/config-conventional --dev
# pnpm
pnpm add @commitlint/cli @commitlint/config-conventional --save-dev</code>Add a
commit-msghook via Husky:
<code>npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'</code>Sample
commitlintconfiguration with custom types:
<code>{
"commitlint": {
"extends": ["@commitlint/config-conventional"],
"rules": {
"type-enum": [2, "always", ["build","chore","ci","docs","feat","fix","perf","refactor","revert","style","test","ui","version"]]
}
}
}</code>Code Checking with ESLint and @typescript-eslint
Install the required packages:
<code># npm
npm install eslint eslint-plugin-react-hooks eslint-plugin-react-refresh @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev
# yarn
yarn add eslint eslint-plugin-react-hooks eslint-plugin-react-refresh @typescript-eslint/parser @typescript-eslint/eslint-plugin --dev
# pnpm
pnpm add eslint eslint-plugin-react-hooks eslint-plugin-react-refresh @typescript-eslint/parser @typescript-eslint/eslint-plugin --save-dev</code>Typical ESLint usage steps: install, initialize configuration, add rules/plugins, run lint, and optionally fix automatically.
Example
.eslintrc.jsconfiguration:
<code>module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended'
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
'@typescript-eslint/ban-ts-comment': 'off'
}
};</code>Combined with lint‑staged:
<code>{
"scripts": {
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"lint:fix": "eslint . --ext ts,tsx --fix"
},
"lint-staged": {
"*.(ts|tsx)": ["eslint --quiet"]
}
}</code>Code Formatting with Prettier
Prettier automatically formats code to a consistent style.
Installation:
<code># npm
npm install prettier --save-dev
# yarn
yarn add prettier --dev
# pnpm
pnpm add prettier --save-dev</code>Configuration example in
package.json:
<code>{
"prettier": {
"trailingComma": "all",
"arrowParens": "always",
"printWidth": 120
}
}</code>Integrate formatting with lint‑staged:
<code>{
"prettier": { ... },
"lint-staged": {
"*.(ts|tsx)": ["eslint --quiet"],
"*.(ts|tsx|json|html)": ["prettier --write"]
}
}</code>Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.