节点应用中Husky故障导致无法提交

问题描述 投票:0回答:2

我已经创建了一个节点应用程序并安装了Husky7.0.4和lint-staged12.3.5

以下是配置 .husky/预提交 --- 文件

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run pre-commit

以下是package.json配置

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon --exec babel-node index.js",
    "build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",
    "prettier:check": "prettier --check .",
    "prettier:fix": "prettier --write .",
    "lint:check": "eslint .",
    "lint:fix": "eslint --fix .",
    "pre-commit": "lint-staged",
    "prepare": "husky install"
  },
  "lint-staged": {
    "*.js": [
      "lint:check",
      "lint:fix",
      "prettier:fix"
    ]
  },
  "dependencies": {
    "lint-staged": "^12.3.5",
  },
  "devDependencies": {
    "@babel/cli": "^7.17.6",
    "@babel/core": "^7.17.5",
    "@babel/node": "^7.16.8",
    "@babel/preset-env": "^7.16.11",
    "eslint": "^8.10.0",
    "eslint-config-prettier": "^8.5.0",
    "husky": "^7.0.4",
    "nodemon": "^2.0.15",
    "prettier": "^2.5.1"
  } 

但是每次我提交代码时都会收到以下错误

> [email protected] pre-commit
> lint-staged

✔ Preparing lint-staged...
⚠ Running tasks for staged files...
  ❯ package.json — 1 file
    ❯ *.js — 1 file
      ✖ lint:check [ENOENT]
      ◼ lint:fix
      ◼ prettier:fix
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...

✖ lint:check failed without output (ENOENT).
husky - pre-commit hook exited with code 1 (error)  
git package.json lint-staged git-husky
2个回答
5
投票

我可以通过更改 lint 阶段的任务来解决问题

"lint-staged": {  
  "*.js": [ 
      "lint:check", 
      "lint:fix", 
      "prettier:fix" 
  ] 
 } 

"lint-staged": { 
  "*.js": [ 
    "npm run lint:check", 
    "npm run lint:fix", 
    "npm run prettier:fix" 
   ] 
 }

0
投票

我可以通过更改来解决问题

"lint-staged": {
    "*.{js,jsx,ts,tsx}": [
      "yarn lint:fix",
      "yarn prettier:check",
      
      "git add"
    ]
  },
© www.soinside.com 2019 - 2024. All rights reserved.