목차

NVM

설치

사용

# node.js 버전 목록 보기
nvm ls-remote
 
# 설치
nvm install v0.11.15
 
# 최신 LTS 설치
nvm install --lts
 
# 사용 - 가장 최근 0.11.x 버전 사용
nvm use 0.11 
 
# 현재 사용중인 버전 확인
nvm current
 
# 전역 기본 버전 지정
nvm alias default v7.7.1
 
# 운영체제에 깔린 버전을 기본으로
nvm alias default system

.nvmrc

nvm use <node-version>
cd targetdir
node -v > .nvmrc

.nvmrc 자동 인식 - bash

autonvm() {
	nvm_path=$(nvm_find_up .nvmrc | tr -d '\n')
 
	# If there are no .nvmrc file, use the default nvm version
	if [[ ! $nvm_path = *[^[:space:]]* ]]; then
 
		if [[ -z $nvm_default_version ]]; then
			nvm_default_version=$(nvm version default);
		fi
 
		# If there is no default version, set it to `node`
		# This will use the latest version on your machine
		if [[ $nvm_default_version == "N/A" ]]; then
			nvm alias default node;
			nvm_default_version=$(nvm version default);
		fi
 
		# If the current version is not the default version, set it to use the default version
		if [[ $(nvm current) != $nvm_default_version ]]; then
			nvm use default;
		fi
 
	elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then
		nvm_version=$(<"$nvm_path"/.nvmrc)
 
		# `nvm ls` will check all locally-available versions
		# If there are multiple matching versions, take the latest one
		# Remove the `->` and `*` characters and spaces
		# `nvm_local_version` will be `N/A` if no local versions are found
		if [[ -z $nvm_local_version ]]; then
			nvm_local_version=$(nvm ls --no-colors "$nvm_version" | tail -1 | tr -d '\->*' | tr -d '[:space:]')
		fi
 
		# If it is not already installed, install it
		# `nvm install` will implicitly use the newly-installed version
		if [[ "$nvm_local_version" == "N/A" ]]; then
			nvm install "$nvm_version";
		elif [[ $(nvm current) != "$nvm_local_version" ]]; then
			nvm use "$nvm_version";
		fi
	fi
}
export PROMPT_COMMAND="$PROMPT_COMMAND; autonvm"