Husky 预提交不会在 create-react 应用程序中触发
Husky pre-commit doesn't fire in create-react app
我安装了 husky 以自动将我的代码格式化为 CRA docs 所说的。当我尝试提交文件时,预提交操作不会触发并且 未格式化的文件被提交 。我能用它做什么?
我没有做任何没有写在 CRA 文档中的与 Prettier 或 Husky 相关的事情:只安装了 prettier、husky 和 lint-staged,并在 package.json 中添加了一些代码。我也在那里看到了一些类似的问题,但答案主要与 Node 和 Git 版本有关,这不是我的情况。
节点 版本:v14.17.5
Git 版本:2.32.0.windows.2
package.json:
{
"name": "ts-react-course",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.4.0",
"@types/node": "^16.11.19",
"@types/react": "^17.0.38",
"@types/react-dom": "^17.0.11",
"axios": "^0.24.0",
"husky": "^7.0.4",
"lint-staged": "^12.1.7",
"prettier": "^2.5.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.2.1",
"react-scripts": "5.0.0",
"types": "^0.1.1",
"typescript": "^4.5.4",
"web-vitals": "^2.1.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"prepare": "husky install"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
"prettier --write"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Husky 的 API has dropped conventional JS configuration support 和 create-react-app
的文档已经过时,因为它显示了配置它的旧方法。当您使用 npm install husky
安装 Husky 时,您安装的是最新的 Husky 版本,而不是旧版本。
建议的解决方案
npx husky install
npm set-script prepare "husky install"
npx husky add .husky/pre-commit "npx lint-staged"
这里是 create-react-app
存储库中的相关 GitHub 问题,以防您想了解最新的修复:https://github.com/facebook/create-react-app/issues/11568
我安装了 husky 以自动将我的代码格式化为 CRA docs 所说的。当我尝试提交文件时,预提交操作不会触发并且 未格式化的文件被提交 。我能用它做什么?
我没有做任何没有写在 CRA 文档中的与 Prettier 或 Husky 相关的事情:只安装了 prettier、husky 和 lint-staged,并在 package.json 中添加了一些代码。我也在那里看到了一些类似的问题,但答案主要与 Node 和 Git 版本有关,这不是我的情况。
节点 版本:v14.17.5
Git 版本:2.32.0.windows.2
package.json:
{
"name": "ts-react-course",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/user-event": "^13.5.0",
"@types/jest": "^27.4.0",
"@types/node": "^16.11.19",
"@types/react": "^17.0.38",
"@types/react-dom": "^17.0.11",
"axios": "^0.24.0",
"husky": "^7.0.4",
"lint-staged": "^12.1.7",
"prettier": "^2.5.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-router-dom": "^6.2.1",
"react-scripts": "5.0.0",
"types": "^0.1.1",
"typescript": "^4.5.4",
"web-vitals": "^2.1.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"prepare": "husky install"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"src/**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
"prettier --write"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Husky 的 API has dropped conventional JS configuration support 和 create-react-app
的文档已经过时,因为它显示了配置它的旧方法。当您使用 npm install husky
安装 Husky 时,您安装的是最新的 Husky 版本,而不是旧版本。
建议的解决方案
npx husky install
npm set-script prepare "husky install"
npx husky add .husky/pre-commit "npx lint-staged"
这里是 create-react-app
存储库中的相关 GitHub 问题,以防您想了解最新的修复:https://github.com/facebook/create-react-app/issues/11568