pre-commit/hook: No such file or directory

69,764

Solution 1

pre-commit hook runs first when you try to commit changes, it can be used to do certain checks, tests, conditions. In this case, clearly, you don't have it, that's why it's complaining.

Go to .git/hooks directory and remove pre-commit file, as its trying to reference to node_modules/pre-commit/hook. It should resolve it.

Other option is append your commit with option: --no-verify, it will make the commit without checking.

Solution 2

You should delete node_modules folder and install again running npm install or yarn install. After it, you probably solve the problem.

Solution 3

If the script files are actually present, then the cause may be that the first line of the script should be something like:

#!/bin/sh

Otherwise you get the same error.

Any file will work -- e.g., /bin/bash, /usr/local/bin/python3 -- as long as it exists and is executable.

Solution 4

Working from macOS, my issue was that Python3 was originally installed via Homebrew, and after I upgraded Python3 via brew update && brew upgrade, I got the same error.

To fix this, I needed to update the symbolic links in the directory that the shebang (#!) line in the .git/hooks/pre-commit file points to.

Here's how I fixed this in my environment:

  1. Look at the contents of the .git/hooks/pre-commit:

    cat .git/hooks/pre-commit
    

    The first few lines should look something like:

    #!/usr/local/opt/pre-commit/libexec/bin/python3
    # File generated by pre-commit: https://pre-commit.com
    # ID: 0123456789abcdef0123456789abcdef
    import os
    import sys
    

    Take note of the path to the python3 executable in the shebang line:

    /usr/local/opt/pre-commit/libexec/bin/
    
  2. cd into that directory.

    cd /usr/local/opt/pre-commit/libexec/bin/
    
  3. Take at look at the Python symlinks:

    ls -l | grep python
    

    You'll see some symlinks that are probably broken:

    lrwxr-xr-x  1 user  group    91 Apr  5 13:33 python -> /usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    lrwxr-xr-x  1 user  group    91 Apr  5 13:33 python3.9 -> /usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    lrwxr-xr-x  1 user  group    91 Apr  5 13:33 python3 -> /usr/local/Cellar/[email protected]/3.9.2_4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    

    Quick note: In case something goes wrong or my answer isn't the solution, you might want to back up these symlinks first by running:

    mv python python.bak
    
    mv python3.9 python3.9.bak
    
    mv python3 python3.bak
    
  4. Update the symbolic links using ln -s [PATH] [LINK], where [PATH] is the location of the Homebrew-updated Python3 executable and [LINK] is python, python3.9, and python3:

    ln -s /usr/local/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9 python
    
    ln -s /usr/local/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9 python3.9
    
    ln -s /usr/local/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9 python3
    
  5. When you've done that, list your Python symlinks again.

    ls -l | grep python
    

    You should see the updated symlinks, and git commit should now work.

    lrwxr-xr-x  1 user  group    89 Apr  6 13:58 python -> /usr/local/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    lrwxr-xr-x  1 user  group    89 Apr  6 13:58 python3 -> /usr/local/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    lrwxr-xr-x  1 user  group    89 Apr  6 13:58 python3.9 -> /usr/local/Cellar/[email protected]/3.9.4/Frameworks/Python.framework/Versions/3.9/bin/python3.9
    
Share:
69,764
Jitendra Vyas
Author by

Jitendra Vyas

Hi, I am Jitendra a front-end developer from India specializing in web standards, accessibility, and usability based development.

Updated on July 09, 2022

Comments

  • Jitendra Vyas
    Jitendra Vyas almost 2 years

    I get this error when I try to commit.

    OS - Latest OSX

    Git version - git version 2.11.0 (Apple Git-81)

    .git/hooks/pre-commit: line 2: ./node_modules/pre-commit/hook: No such file or directory
    
    • c3st7n
      c3st7n almost 7 years
      You have a pre-commit hook defined (in .git/hooks/pre-commit) and it is trying to reference ./node_modules/pre-commit/hook which doesn't exist. If the hook is no longer needed you can just delete it.
  • Greg
    Greg over 6 years
    I got exactly the same error which means suggests something is wrong with the gem webpacker and webpack installation process; because that's what I just had done. But odd that the pre-commit file was last modified six months ago. But the removal of the pre-commit file fixed my problem.
  • Komal Goyani
    Komal Goyani over 4 years
    I got same issue but it is not solved by removing pre-commit file. Than i tried to commit by command in terminal and add --no-verify command at last so my command is git commit -m "message" --no-verify. It is working for me.
  • Rickchip
    Rickchip almost 4 years
    This can happen if Lint is setup to enforce formatting rules. As @Mutant and @KomalGoyani mentioned, you can bypass the Lint check using git commit -n (or --no-verify)
  • drecunion
    drecunion almost 3 years
    it solves the problem temporarily, but it also skips your pre-commit task and doesn't really resolve the issue. Its just a workaround to bypass the error.