nvm 설치 후에 느려진 터미널 개선하기
🐌

nvm 설치 후에 느려진 터미널 개선하기

Published
November 10, 2024
Tags
nvm
cli

개요

nvm은 Node Version Manager로 다양한 버전의 노드를 손쉽게 전환할 수 있도록 해준다. 사용이 간편하지만, 얼마 전부터 터미널 세션을 새로 만들때 묘하게 느려진 속도를 보면서 하나하나 조사하다가 nvm이 문제임을 알게 되었다. 그래서 이를 개선한 내용을 공유하고자 한다.

nvm

nvm은 다음과 같이 손쉽게 설치할 수 있다.
brew install nvm
설치 후에는 다음과 같이 알림이 뜨는데, 쉘 프로파일이나 rc 파일에 수동으로 설정을 추가하도록 안내하고 있다.
Please note that upstream has asked us to make explicit managing nvm via Homebrew is unsupported by them and you should check any problems against the standard nvm install method prior to reporting. You should create NVM's working directory if it doesn't exist: mkdir ~/.nvm Add the following to your shell profile e.g. ~/.profile or ~/.zshrc: export NVM_DIR="$HOME/.nvm" [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm [ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion You can set $NVM_DIR to any location, but leaving it unchanged from /opt/homebrew/Cellar/nvm/0.40.1 will destroy any nvm-installed Node installations upon upgrade/reinstall. Type `nvm help` for further information.
안내한 바와 같이, ~/.zshrc에 설정을 추가한다.
notion image

문제

설정을 추가하고 새 터미널을 열면서 시간이 전보다 오래 걸리는 것을 알게 되었다. 테스트를 위해서 time 커맨드를 이용하여 새 터미널을 열고 닫는 시간을 측정해보자.
> time zsh -i -c exit zsh -i -c exit 0.26s user 0.47s system 86% cpu 0.852 total > time zsh -i -c exit zsh -i -c exit 0.18s user 0.36s system 88% cpu 0.604 total > time zsh -i -c exit zsh -i -c exit 0.18s user 0.37s system 83% cpu 0.662 total
3회를 측정했는데, 대략 0.6~0.8초 정도 소요되는 것을 알 수 있다. 이러면 안되는데… 원인을 분석해보니, nvm을 로딩하는 과정이 생각보다 느리다는 것을 알 수 있었다. 로딩은 앞서 설정의 두 번째 줄에 해당한다.

해결

그렇다면, 이를 해결할 방법은 어떻게 되는가? 로딩 설정을 제외하고 새로운 설정을 추가하자.
export NVM_DIR="$HOME/.nvm" [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm alias nvm="unalias nvm; [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && . "$NVM_DIR/nvm.sh"; nvm $@"
다음과 같이 수정하면, nvm 명령을 수행할 때만 로딩하게 되므로 터미널 생성 시간을 잡아먹지 않게 된다.
> time zsh -i -c exit zsh -i -c exit 0.00s user 0.01s system 70% cpu 0.018 total > time zsh -i -c exit zsh -i -c exit 0.00s user 0.01s system 72% cpu 0.015 total > time zsh -i -c exit zsh -i -c exit 0.00s user 0.01s system 74% cpu 0.017 total
일반적으로 이렇게 나오는 것이 정상이다.

마무리

nvm이 터미널을 느리게 하여 이를 해소하는 방법을 알아보았다. 다만, 여기에는 한 가지 문제가 있다. nvm 명령을 입력할 때에만 로딩한다는 뜻은, node, npm 명령이 nvm을 실행하기 전에는 전혀 동작하지 않는다는 것이다. 어떻게 보면 장점이자 단점인데,
  • 장점 : 터미널에서 명시적으로 nvm을 통해 노드 버전을 알려줘야만 동작한다.
  • 단점 : 매번 같은 노드 버전을 사용하는데, 기본값 설정이 되지 않아서 번거롭다.
이러한 점을 감안하여 설정하길 바란다.

참고