Git pre-commit hook failing in GitHub for mac (works on command line)

Resolved. As globally installed node modules end up under /usr/local/bin I simply needed to add the following at the beginning of my pre-commit:

PATH=$PATH:/usr/local/bin:/usr/local/sbin

i.e. appending both /usr/local/bin and /usr/local/sbin to PATH at the point of execution.


The $PATH variable that is available in GUI environments like Github Desktop and Sourcetree is different than the one available in the terminal. By default the $PATH available in the GUI environments can't find your node modules. As was stated in the previous answer, you can ensure that /usr/local/bin is in the path by adding

PATH=$PATH:/usr/local/bin:/usr/local/sbin

In my case this did not work because I am using Node Version Manager, which stores different versions of Node and makes it easy to upgrade and switch Node versions. It stores your node_modules for each version of Node in a separate file. Here is the code I used to get around this problem:

#!/usr/bin/env bash

PATH="/usr/local/bin:$PATH"

if [ -f $HOME/.nvm/nvm.sh ]
then
  . $HOME/.nvm/nvm.sh
  PATH="$HOME/.nvm/versions/node/$(nvm current)/bin:$PATH"
fi

This checks for NVM, and if it exists, loads it and uses it to find the path to the node modules for the currently used version of Node. If you are only trying to access node modules and don't need to get at anything special you can skip adding the sbin folder, e.g. /usr/local/sbin