Skip to content

Instantly share code, notes, and snippets.

@loucyx
Last active April 18, 2024 23:17
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save loucyx/e239528fbcc4bba9ae2ef406f197df0c to your computer and use it in GitHub Desktop.
Save loucyx/e239528fbcc4bba9ae2ef406f197df0c to your computer and use it in GitHub Desktop.
if [ -s "$HOME/.nvm/nvm.sh" ] && [ ! "$(type -f __init_nvm)" = function ]; then
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
declare -a __node_commands=(nvm `find -L $NVM_DIR/versions/*/*/bin -type f -exec basename {} \; | sort -u`)
function __init_nvm() {
for i in "${__node_commands[@]}"; do unalias $i; done
. "$NVM_DIR"/nvm.sh
unset __node_commands
unset -f __init_nvm
}
for i in "${__node_commands[@]}"; do alias $i='__init_nvm && '$i; done
fi
@shawntax
Copy link

Great snippet!

@jcklpe
Copy link

jcklpe commented Nov 24, 2018

This didn't fix my nvm slowness per se but it did get it to stop complaining every time I loaded it up that N/A wasn't installed in N/A.

@lamyergeier
Copy link

lamyergeier commented Jan 26, 2019

@lukeshiru Why did you change -t to -f ? Can't find the options in the man page.

@loucyx
Copy link
Author

loucyx commented Mar 15, 2019

Sorry for the late response @anishmittal2020, I just noticed your comment: -t isn't valid in zsh, so you need to use -f instead. Maybe this link is useful for you: https://serverfault.com/questions/879222/get-on-zsh-the-same-result-you-get-when-executing-type-t-on-bash

@katafractari
Copy link

This has fixed my nvm slowness!

@loucyx
Copy link
Author

loucyx commented Nov 19, 2019

I'm glad, @katafractari!

@tovbinm
Copy link

tovbinm commented Apr 10, 2020

Thank you!

@lblblong
Copy link

This is great and solves the problem of slow startup

@kiprasmel
Copy link

kiprasmel commented Sep 20, 2021

thank you. for macos osx, I didn't have nvm.sh in $NVM_HOME, so here's my fix:

# nvm (fixed):
# see https://gist.github.com/lukeshiru/e239528fbcc4bba9ae2ef406f197df0c#gistcomment-3898951
if ([ -s "$HOME/.nvm/nvm.sh" ] || [ -s "/usr/local/opt/nvm/nvm.sh" ]) && [ ! "$(type -f __init_nvm)" = function ]; then
	export NVM_DIR="$HOME/.nvm"
	# [ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"
	[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion
	declare -a __node_commands=(nvm `find -L $NVM_DIR/versions/*/*/bin -type f -exec basename {} \; | sort -u`)
	function __init_nvm() {
		for i in "${__node_commands[@]}"; do unalias $i; done
		# . "$NVM_DIR"/nvm.sh
		[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"  # This loads nvm
		unset __node_commands
		unset -f __init_nvm
	}
	for i in "${__node_commands[@]}"; do alias $i='__init_nvm && '$i; done
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment