====== NVM ====== * [[:node.js|node.js]] [[https://github.com/creationix/nvm|Node.js Version Manager]] * [[node.js:n|n]] * [[node.js:nve|nve]] * [[node.js:nvs|nvs - Node Version Switcher]] * [[node.js:nodeenv|nodeenv]] ===== 설치 ===== * 가급적 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 ===== * 프로젝트 / 디렉토리 단위로 자동 node 버전 지정 * ''.nvmrc'' 파일을 프로젝트 디렉토리 최상위에 만들고 버전 지정. * [[https://github.com/nvm-sh/nvm#automatically-call-nvm-use|nvm-sh/nvm: Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions]] : ''.nvmrc''가 있을 때 자동으로 ''nvm use'' 호출 * 혹은 [[node.js:avn|avn]] 사용. * https://github.com/direnv/direnv/wiki/Node | [[linux:direnv|direnv]] and node * [[https://stackoverflow.com/questions/23556330/run-nvm-use-automatically-every-time-theres-a-nvmrc-file-on-the-directory|javascript - run `nvm use` automatically every time there's a .nvmrc file on the directory - Stack Overflow]] nvm use cd targetdir node -v > .nvmrc * ''nvm use'' : 현재 디렉토리의 ''.nvmrc'' 에 있는 버전을 사용 * ''nvm install'' : 현재 디렉토리의 ''.nvmrc''에 있는 버전을 설치 ==== .nvmrc 자동 인식 - bash ==== * [[https://weston.ruter.net/2022/05/19/auto-nvm-use/|Auto nvm use - Weston Ruter]] * ''.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"