상현에 하루하루
개발자의 하루

Node 버전 쉽게 전환하기 (homebrew)

( 업데이트: )

모든 언어가 그렇지만 개발을 하다 보면 프로젝트마다 언어, 의존성 등의 버전을 다르게 해서 개발할 때가 있습니다.

노드 버전을 손쉽게 변경하고 관리할 수 있는 프로그램들을 설치해서 사용해 보도록하겠습니다.

nodebrew nvm 이 두가지를 제일 많이 사용한다.

설치는 모두 homebrew로 진행할 것이다.

nodebrew

설치

brew install nodebrewCode language: Bash (bash)
nobrew setup
Fetching nodebrew...
Instaled nodebrew in $HOME/.nodebrew

========================================
Export a path to nodebrew:

export PATH=$HOME/.nodebrew/current/bin:$PATH
========================================Code language: Bash (bash)

개발환경에서 사용할 node 버전을 지정

nodebrew install-binary v6.5.0
nodebrew use v6.5.0Code language: Bash (bash)

error

node -v
v16.15.0Code language: Bash (bash)

🤔 왜 설치한 v6.5.0으로 변경되지 않은 걸까?

nodebrew에서 설치한 node의 경로로 path가 설정되지 않았기 때문이다.

$HOME/.nodebrew/current/bin/node -v
v6.5.0
echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> $HOME/.zshrc
node -v
v6.5.0Code language: Bash (bash)

nvm

brew install nvmCode language: Bash (bash)
echo 'export NVM_DIR=~/.nvm' >> $HOME/.zshrc
echo 'source $(brew --prefix nvm)/nvm.sh' >> $HOME/.zsrhcCode language: Bash (bash)

터미널을 재시작한 후, 아래 명령어를 실행하여 NVM의 버전을 확인합니다.

nvm --versionCode language: Bash (bash)

최신 LTS node버전 설치

nvm install --lts
nvm uss {특정버전}Code language: Bash (bash)

프로젝트별 node 버전 관리

프로젝트 루트에 .nvmrc를 생성합니다. 그리고 사용할 버전을 내용을 추가합니다.

# .nvmrc
16.15.0Code language: Bash (bash)

.nvmrc 사용하기

프로젝트 루트에서 nvm use 명령어로 실행하면 Local에서 해당 버전일 설치되어있는 경우는

Found '/test-project/.nvmrc' with version <16.15.0>
Now using node v16.15.0 (npm v6.13.4)Code language: Bash (bash)

만약 Local에 해당 버전이 설치되어있지 않다면

Found '/test-project/.nvmrc' with version <8.9.0>
N/A: version "8.9.0 -> N/A" is not yet installed.Code language: Bash (bash)
nvm installCode language: Bash (bash)

해당 명령어로 .nvmrc의 버전을 설치할 수 있다.

...
Checksums matched!
Now using node v16.15.0 (npm v6.3.0)Code language: Bash (bash)