Skip to content

Instantly share code, notes, and snippets.

@Francois-Esquire
Last active May 22, 2018 22:01
Show Gist options
  • Save Francois-Esquire/bfcfbabada71dbf6da1904b5db1ff69a to your computer and use it in GitHub Desktop.
Save Francois-Esquire/bfcfbabada71dbf6da1904b5db1ff69a to your computer and use it in GitHub Desktop.
loading ./node_modules/.bin on the fly | only possible with zsh terminals, avoids setting traps to cd, etc...
# for loading node module bin files into PATH
NPM_LOCAL_MODULES=/dev/null
function npm_bin_loader() {
CURRENT_NPM_MODULE=$NPM_LOCAL_MODULES
if [ -d "node_modules" ]
then
NPM_LOCAL_MODULES="$(pwd)/node_modules/.bin"
else
NPM_LOCAL_MODULES=/dev/null
fi
if [ $NPM_LOCAL_MODULES != $CURRENT_NPM_MODULE ]
then
PATH=$(echo $PATH | sed -e 's|'$CURRENT_NPM_MODULE'|'$NPM_LOCAL_MODULES'|g')
fi
# just to check the path
echo $PATH
}
# adding our helper to the change pwd hook
chpwd_functions+=("npm_bin_loader")
# export the path
export PATH=$NPM_LOCAL_MODULES:$PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment