# [Node.js] 使用 nvm 管理 Node.js 版本
###### tags: `Node.js`
## 安裝
```
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.0/install.sh | bash
```
### 安裝成功
關掉目前的 Terminal,重開新的,再使用 nvm 的指令
### 安裝失敗 - Profile not found.
失敗訊息如下,找不到 ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile
```bash
=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
OR
=> Append the following lines to the correct file yourself:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
```
1. 先用 ls 檢查電腦中是否真的沒有上述檔案,也可以直接到使用者資料夾,顯示隱藏的檔案後檢查(macOS 同時按下 `shift` + `command` + `.`)
```bash
ls ~/.bashrc
ls ~/.bash_profile
ls ~/.zshrc
ls ~/.profile
```
2. 若有找到其中一個檔案,在檔案內加上
```
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
```
3. 若都找不到,則創建其中一個檔案
:::warning
Since macOS 10.15, the default shell is zsh and nvm will look for .zshrc to update, none is installed by default. Create one with touch ~/.zshrc and run the install script again.
:::
```bash
touch ~/.zshrc
```
4. 重新執行安裝nvm
## 指令
| 指令 | 功能 |
| --- | --- |
| command -v nvm | 確認是否安裝成功,若成功會顯示 nvm |
| nvm -v 或 nvm --version | 顯示 nvm 版本 |
| nvm ls | 顯示目前已安裝的 Node.js 版本清單 |
| nvm ls-remote | 顯示可安裝的 Node.js 版本號清單<br>白色:未安裝;其他顏色:已安裝|
| nvm install node | 安裝最新版的 Node.js |
| nvm install **版本號** | 安裝指定版本的 Node.js |
| node -v | 顯示目前的 Node.js 版本 |
| nvm current | 顯示目前使用的 Node.js 版本 |
| nvm use **版本號** | 切換至指定版本的 Node.js |
| nvm alias default | 顯示預設的 Node.js 版本號 |
| nvm alias default **版本號** | 設定預設的 Node.js 版本號 |
| nvm uninstall **版本號** | 移除指定版本的 Node.js |
第一個安裝的 Node.js 版本會變成預設版本
## 參考網站
* [nvm GitHub](https://github.com/nvm-sh/nvm "Node Version Manager")
* [nvm:安裝、切換不同 Node.js 版本的管理器](https://titangene.github.io/article/nvm.html "nvm:安裝、切換不同 Node.js 版本的管理器")
---
:::info
建立日期:2020-11-13
更新日期:2024-01-21
:::