nvm을 사용하면 node.js 버전을 쉽게 전환할 수 있다. nvm ls 명령어를 사용해서 내가 설치한 node.js 버전 목록을 확인하고 nvm use 명령어를 사용해서 프로젝트에서 요구하는 node.js 버전으로 전환이 가능하다.
그런데 만일 내가 진행하고 있는 프로젝트가 여러 개고 각 프로젝트 별로 요구하는 node.js의 버전이 다르다면 프로젝트 별로 nvm use를 통해 버전을 전환하는 건 꽤 번거로운 일이다. 그렇다면 프로젝트를 실행할 때마다 자동으로 각 프로젝트가 요구하는 node.js 버전으로 전환해주는 방법이 없을까?
이 글에서는 프로젝트별 Node.js 버전 관리에 대해 개발자가 신경쓰지 않아도 되는 스크립트를 설정하는 과정을 다룰 것이다.
nvm이 설치되었음을 가정한다. nvm을 설치하지 않았다면 아래 글을 참고하자
[macOS] Node.js & npm & nvm 설치
1. nvm(node version manager) 설치homebrew를 사용하지 않고 nvm을 사용하여 Node.js 설치를 진행하고자 한다.그 이유는 nvm을 사용하면 여러 버전의 Node.js를 쉽게 설치하고 전환할 수 있어, 다양한 프로젝
kimyeongseo.tistory.com
1. Node.js 버전 자동으로 관리해주는 nvm 스크립트 설정하기
1) ~/.zshrc 파일 열기
$ nano ~/.zshrc
2) ~/.zshrc 파일 마지막에 다음 내용 추가하기
# NVM 자동 전환 함수
autoload -U add-zsh-hook
load-nvmrc() {
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(cat "${nvmrc_path}")
local installed_version=$(nvm version "$nvmrc_node_version")
if [ "$installed_version" = "N/A" ]; then
echo "Required Node version $nvmrc_node_version is not installed."
read "response?Do you want to install it? (y/N) "
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
nvm install "$nvmrc_node_version"
nvm use "$nvmrc_node_version"
else
echo "Using default version instead."
nvm use default
fi
elif [ "$installed_version" != "$(nvm version)" ]; then
nvm use "$nvmrc_node_version"
fi
elif [ "$(nvm version)" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
스크립트에 작성한 함수의 내용은 다음과 같다.
.nvmrc 파일에 명시된 Node.js 버전이 설치되어 있는지 확인하고 필요한 버전이 설치되어 있지 않은 경우,
사용자에게 설치 여부(y/N)를 물어본다.
사용자가 동의(y)하면 필요한 버전을 자동으로 설치하고 사용한다.
사용자가 설치를 원하지 않을 경우(N), 기본 버전을 사용한다.
3) 설정 적용하기
$ source ~/.zshrc
여기까지만 설정하면 프로젝트 폴더 실행 시 프롬프트 출력 관련 오류가 발생한다. ( [WARNING]: Console output during zsh initialization detected. )
2. 문제 해결 - [WARNING]: Console output during zsh initialization detected.
[WARNING]: Console output during zsh initialization detected.
When using Powerlevel10k with instant prompt, console output during zsh initialization may indicate issues.
You can:
- Recommended: Change ~/.zshrc so that it does not perform console I/O after the instant prompt preamble. See the link below for details.
* You will not see this error message again.
* Zsh will start quickly and prompt will update smoothly.
- Suppress this warning either by running p10k configure or by manually defining the following parameter:
typeset -g POWERLEVEL9K_INSTANT_PROMPT=quiet
* You will not see this error message again.
* Zsh will start quickly but prompt will jump down after initialization.
- Disable instant prompt either by running p10k configure or by manually defining the following parameter:
typeset -g POWERLEVEL9K_INSTANT_PROMPT=off
* You will not see this error message again.
* Zsh will start slowly.
- Do nothing.
* You will see this error message every time you start zsh.
* Zsh will start quickly but prompt will jump down after initialization. For details, see:
https://github.com/romkatv/powerlevel10k#instant-prompt
-- console output produced during zsh initialization follows --
1) ~/.p10k.zsh 파일 열기
$ nano ~/.p10k.zsh
2) ~/.p10k.zsh 파일 수정하기 ( verbose → off)
typeset -g POWERLEVEL9K_INSTANT_PROMPT=off
3) 설정 적용하기
$ source ~/.p10k.zsh
4) 결과
🔗 References
Using Node Version Manager and .nvmrc to automatically switch Node versions per project
'⚙️ Setup' 카테고리의 다른 글
[VSCode] VSCode에서 Prettier 설치하여 자동으로 코드 정렬 설정하기 (0) | 2024.09.03 |
---|---|
[macOS] Front-end 개발 환경 세팅하기 (3) | 2024.09.02 |
[macOS] Node.js & npm & nvm 설치 (0) | 2024.08.31 |
[macOS] VS Code에 iTerm2 터미널 연동 및 폰트 깨짐 해결 (0) | 2024.08.30 |
[macOS] Github 계정 Token 사용하여 터미널(iTerm2) 연동하기 (0) | 2024.08.29 |