Is this a right way to use husky + lint-staged? - Stack Overflow
eslint - How do I get lint-staged working with Husky version 6 - Stack Overflow
javascript - lint-staged not running on precommit
husky v9 lint-staged not triggered on pre-commit
Videos
You can indeed run multiple commands with lint-staged and if one of them fails, you will get the correct exit code as shown in the example below. This works via the new husky hooks system:
With a configuration like this in package.json:
"husky": {
"hooks": {
"pre-commit": "lint-staged"
},
},
"lint-staged": {
"src/**/*.{js,jsx,ts,tsx,json,css}": [
"prettier --write",
"eslint --fix src/",
"tslint --fix --project .",
"git add"
]
},
The configuration runs prettier, eslint and tslint - you would get the following error on linting problems:
husky > pre-commit (node v8.12.0)
โ Stashing changes... [skipped]
โ No partially staged files found...
โฏ Running linters...
โฏ Running tasks for src/**/*.{js,jsx,ts,tsx,json,css}
prettier --write
eslint --fix src/
tslint --fix --project .
git add
prettier --write found some errors. Please fix them and try committing again.
...
husky > pre-commit hook failed (add --no-verify to bypass)
The last line shows you that git's own pre-commit hook failed and thus your changed won't get commited (if they are not fixable).
Some files I use, in case someone can be interested:
.prettierrc
{
"printWidth": 120,
"proseWrap": "preserve",
"semi": false,
"singleQuote": true,
"useTabs": false,
"tabWidth": 2,
"arrowParens": "avoid",
"trailingComma": "es5"
}
.lintstagedrc
{
"**/*.+(js|md)": [
"prettier --write",
"eslint --fix src/",
"git add"
]
}
.prettierignore
node_modules
coverage
.huskyrc
{
"hooks": {
"pre-commit": "lint-staged"
}
}
Making lint-staged working with Husky version 6 by adding:
// .husky/pre-commit
npm run pre-commit
and:
// package.json
{
"scripts": {
"pre-commit": "lint-staged"
}
}
Based on this husky github issue, just do these steps to make it work(we did it and it is working fine):
npx husky-inityarnnpx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'yarn add @commitlint/config-conventional @commitlint/cli --dev
.commitlintrc.json:
{
"extends": ["@commitlint/config-conventional"]
}
.lintstagedrc:
{
"src/**/*.+(js|json|ts|tsx)": [
"eslint"
],
"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
"prettier --write"
]
}
.husky/pre-commit:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
yarn pre-commit-lint
Finally, add pre-commit-lint script to package.json:
{
"name": "pwa-react-scaffold",
"version": "0.1.0",
"private": true,
"author": "SeyyedMahdi Hassanpour <[email protected]>",
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"analyze": "yarn build && source-map-explorer 'build/static/js/*.js'",
"lint": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx .",
"check-types": "tsc",
"prettier": "prettier --ignore-path .gitignore \"src/**/*.+(js|jsx|json|ts|tsx)\"",
"format": "yarn prettier --write",
"check-format": "yarn prettier --list-different",
"validate": "npm-run-all --parallel check-types check-format lint build",
"prepare": "husky install",
"pre-commit-lint": "yarn check-types && yarn lint-staged"
},
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "^16.9.53",
"@types/react-dom": "^16.9.8",
"node-sass": "^5.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"source-map-explorer": "^2.5.2",
"typescript": "^4.0.3",
"web-vitals": "^0.2.4",
"workbox-background-sync": "^5.1.3",
"workbox-broadcast-update": "^5.1.3",
"workbox-cacheable-response": "^5.1.3",
"workbox-core": "^5.1.3",
"workbox-expiration": "^5.1.3",
"workbox-google-analytics": "^5.1.3",
"workbox-navigation-preload": "^5.1.3",
"workbox-precaching": "^5.1.3",
"workbox-range-requests": "^5.1.3",
"workbox-routing": "^5.1.3",
"workbox-strategies": "^5.1.3",
"workbox-streams": "^5.1.3"
},
"devDependencies": {
"@commitlint/cli": "^12.1.1",
"@commitlint/config-conventional": "^12.1.1",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"eslint-config-prettier": "^8.2.0",
"eslint-plugin-jest-dom": "^3.8.0",
"eslint-plugin-testing-library": "^4.1.0",
"eslint-plugin-unused-imports": "^1.1.1",
"husky": "^6.0.0",
"lint-staged": "^10.5.4",
"npm-run-all": "^4.1.5",
"prettier": "^2.2.1"
}
}
I tried so many solutions on here but a combination finally worked!
- Make sure Husky v4 is installed. v6 was never triggering for me.
- Check the output of
git config core.hooksPath. This should not return anything. If it does run,
git config --unset core.hookspath
And FINALLY it worked!
In 2021
Sometimes hooks are not added by husky so you need to add it using a simple easy hack.
You need to uninstall husky first after that install V4 of husky because it ensures that your hooks are correctly installed and after that install the latest version of husky so you get the latest updates.
NPM
npm uninstall husky
npm install -D husky@4
npm install -D husky
YARN
yarn remove husky
yarn add -D husky@4
yarn add -D husky
If sometimes above trick not works, so let's add the hook into husky, below mention method is used only in V6 and I am showing the husky with lint-staged example.
NPM
npm install -D husky
npm set-script prepare "husky install" && npm run prepare
npx husky add .husky/pre-commit "npx lint-staged"
git commit -m "added husky and lint-stagged" // here you will notice the lint-staged checking the files with help of husky
YARN
yarn add -D husky
npm set-script prepare "husky install" && yarn prepare
npx husky add .husky/pre-commit "yarn lint-staged"
git commit -m "added husky and lint-stagged" // here you will notice the lint-staged checking the files with help of husky