Skip to content

Instantly share code, notes, and snippets.

@shellscape
Created February 5, 2025 02:44
Show Gist options
  • Save shellscape/dfdbc2944241e157caf9e29a16bbb5c6 to your computer and use it in GitHub Desktop.
Save shellscape/dfdbc2944241e157caf9e29a16bbb5c6 to your computer and use it in GitHub Desktop.
Add local node_modules/.bin to the PATH dynamically
function add_node_modules_bin() {
export _OLD_NODE_PATH="$PATH"
if [[ -d "./node_modules/.bin" ]]; then
export PATH="$PWD/node_modules/.bin:$PATH"
fi
}
function remove_node_modules_bin() {
if [[ -n "$_OLD_NODE_PATH" ]]; then
export PATH="$_OLD_NODE_PATH"
unset _OLD_NODE_PATH
fi
}
# Hook into directory changes
add-zsh-hook chpwd add_node_modules_bin
# Hook that runs when shell starts (new tab/window)
precmd_functions+=(add_node_modules_bin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment