사용자 도구

사이트 도구


node.js:nvm

NVM

설치

  • 가급적 github 의 README 를 보고 따라할 것. 버전마다 달라질 수 있음.
    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
  • .bashrc 등에 추가. 파일 마지막에 자동 추가 되지만 확인 한 번 해볼 것.
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

사용

# 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
  • nvm use : 현재 디렉토리의 .nvmrc 에 있는 버전을 사용
  • nvm install : 현재 디렉토리의 .nvmrc에 있는 버전을 설치

.nvmrc 자동 인식 - bash

  • .bashrc 등에 다음 내용 적용. nvm_find_upnvm에 내장된 함수이다.
  • 아래 스크립트의 문제점은 이미 .nvmrc 디렉토리 밖으로 나온 상태에서 원하는 node 버전을 강제로 지정해도 무조건 자동으로 default 버전으로 바꿔버린다는 점이다.
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"
node.js/nvm.txt · 마지막으로 수정됨: 2023/01/18 22:47 저자 kwon37xi