Javascript lint暂存未在预提交上运行

Javascript lint暂存未在预提交上运行,javascript,git,pre-commit-hook,prettier,lint-staged,Javascript,Git,Pre Commit Hook,Prettier,Lint Staged,prettier未在预提交上运行。这在其他项目中使用了相同的配置,所以我很困惑为什么这次它不起作用 这是my package.json文件的相关部分: "scripts": { "precommit": "lint-staged" }, "lint-staged": { "*.{js,json,css,scss,html,md}": [ "prettier --write", "git add" ] }, 编辑。以下是相关的设计要求: "devDepende

prettier未在预提交上运行。这在其他项目中使用了相同的配置,所以我很困惑为什么这次它不起作用

这是my package.json文件的相关部分:

"scripts": {
    "precommit": "lint-staged"
  },
"lint-staged": {
  "*.{js,json,css,scss,html,md}": [
    "prettier --write",
    "git add"
  ]
},
编辑。以下是相关的设计要求:

"devDependencies": {
  "husky": "^0.14.3",
  "lint-staged": "^7.0.4",
  "prettier": "1.12.0"
},
npm install --save-dev prettier husky lint-staged

重新安装哈士奇,现在似乎正在工作。感谢@mpasko256对您的帮助

您缺少依赖项:

"devDependencies": {
  "husky": "^0.14.3",
  "lint-staged": "^7.0.4",
  "prettier": "1.12.0"
},
npm install --save-dev prettier husky lint-staged

如果它对其他人有帮助:另一种尝试是删除您的
node\u模块
文件夹,然后重新运行
npm安装

我最初在Windows 10机器上的linux子系统中运行了
npm install
。通过bash使用git,一切都很好。我在年切换到git后收到错误 Powershell。卸载和重新安装prettier、husky和lint staged对我来说不起作用


我删除了我的node_modules文件夹,然后从Windows端重新运行npm安装,现在它可以正常工作了。

我也遇到了同样的问题,但我犯了这个错误

我在
husky
对象中添加了
lint staged
对象,但后来意识到我需要在
package.json中添加
lint staged
键值对作为直接键值对

"lint-staged": {
  "*.{js,json,css,scss,html,md}": [
    "prettier --write",
    "git add"
  ]

在我的例子中,问题是有一些现有的钩子,
husky
没有覆盖它们(更多信息)


把它放在这里,以防其他人遇到同样的问题。

我的问题是我运行了“npx mrm lint staged”,就像官方网站所说的那样,但它只在package.json中设置了huskylint staged配置。它不作为依赖项添加或安装它们

我的解决方案是:

  • npm i-D哈士奇皮棉分级

  • npx-mrm-lint-staged


  • 请注意您正在使用的节点版本。Husky需要节点>=10,lint staged需要节点>=10.13

    在配置此脚本之前,您的Husky软件包可能已经在
    节点模块中了。尝试重新安装挂钩,您可以运行:

    npm重建
    
    或者如果:

    npm重建--更新二进制文件
    

    它解决了我的问题。

    我认为你的
    包.json有问题

    "scripts":{
       ...
    },
    "husky": {
        "hooks": {
            "pre-commit": "lint-staged",
            "pre-push": "npm test"
        }
    },
    "lint-staged": {
        "*.ts": ["tslint", "prettier --write", "git add"]
    }
    
    yarn add --dev pre-commit
    
    顺便说一下,在安装了husky之后,只需检查
    .git/hooks/pre-commit
    内容。如果其中没有类似的单词,只需删除
    .git/hooks/pre-commit
    文件并重新安装
    husky
    或运行
    npx-husky
    。因为
    husky
    将跳过修改
    .git/hooks/pre-commit
    文件,如果它不是
    GHook
    相似或
    PreCommit
    相似

    您可以通过以下链接找到它。

    另一种选择是使用
    预提交

    "scripts":{
       ...
    },
    "husky": {
        "hooks": {
            "pre-commit": "lint-staged",
            "pre-push": "npm test"
        }
    },
    "lint-staged": {
        "*.ts": ["tslint", "prettier --write", "git add"]
    }
    
    yarn add --dev pre-commit
    

    这件事发生在我身上,这些答案都没有帮助。所以为了将来参考,这是因为我使用了
    npm@7

    我发现这是husky和npm的一个问题,因为我发现在
    myproject/.git/hooks
    目录中没有
    pre-commit
    文件

    当您安装husky时,它会自动在这样的文件夹中为您发挥其魔力。为此,我必须:

  • 降级至
    npm i-gnpm@6
  • 确保所有组件都是用
    rm-rf node_modules package-lock.json&&npm i重新安装的(您应该在控制台中看到Husky输出)
  • 尽管它不是真的需要,我还是再次执行了
    npx-mrm-lint-staged

  • 最后,它成功了。

    lint阶段v10以后不再需要使用
    git add
    命令。根据文档的描述,它会自动插入到提交中:

    从v10.0.0开始,对最初暂存文件的任何新修改都将自动添加到提交中。如果您的任务以前包含git添加步骤,请删除此步骤。自动行为可确保竞争条件较少,因为尝试同时运行多个git操作通常会导致错误


    对于有此问题且使用Husky 5的用户,挂钩不会自动安装。因此,您可能根本没有
    .git/hooks文件夹中所需的钩子。您需要在package.json中添加
    postinstall
    (推荐),或者在npm安装包后运行
    npx husky install

    或者干脆降级到赫斯基4。如果你和我一样,正在做一个商业项目,又不想成为一个哈士奇赞助商,你就必须这么做


    对于我来说,通过卸载和安装较低版本解决了问题

    npm uninstall husky
    
    npm install -D husky@4          //after this it will work
    

    确保安装了husky

    将以下脚本添加到package.json脚本

    "prepare": "husky install && npx husky add .husky/pre-commit \"npm run lint-fix\"",
    "lint": "eslint ./",
    "lint-fix": "eslint ./ --fix"
    
    您的脚本将看起来像这样

    "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "prepare": "husky install && npx husky add .husky/pre-commit \"npm run lint-fix\"",
        "lint": "eslint ./",
        "lint-fix": "eslint ./ --fix",
        "format": "prettier --write \"**/*.{js,jsx,json,md}\""
      },
    
    运行以下命令

    npm run prepare 
    
    此脚本将在您的工作目录中创建一个.husky文件夹,并使用脚本npm run lint fix将预提交文件添加到其中

    恭喜。。。现在,您可以提交文件,并查看预提交检查您的eslint错误(如果有)

    您可以将下面的行添加到.git ignore文件中

    /.husky
    

    2021年

    有时,husky不会添加钩子,因此需要使用简单的hack添加钩子

    在安装husky的
    V4
    之后,您需要首先卸载husky,因为它可以确保正确安装挂钩,然后安装最新版本的
    husky
    ,以便获得最新更新

    NPM

    npm uninstall husky
    
    npm install -D husky@4
    
    npm install -D husky
    
    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 remove husky
    
    yarn add -D husky@4
    
    yarn add -D husky
    
    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
    
    如果上面的技巧有时不起作用,那么让我们将钩子添加到husky中,下面提到的方法仅在
    V6
    中使用,我将用
    lint staged
    示例演示
    husky

    NPM

    npm uninstall husky
    
    npm install -D husky@4
    
    npm install -D husky
    
    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 remove husky
    
    yarn add -D husky@4
    
    yarn add -D husky
    
    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
    
    我试过了