curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
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
파일을 프로젝트 디렉토리 최상위에 만들고 버전 지정..nvmrc
가 있을 때 자동으로 nvm use
호출nvm use <node-version> cd targetdir node -v > .nvmrc
nvm use
: 현재 디렉토리의 .nvmrc
에 있는 버전을 사용nvm install
: 현재 디렉토리의 .nvmrc
에 있는 버전을 설치.bashrc
등에 다음 내용 적용. nvm_find_up
은 nvm
에 내장된 함수이다..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"