Node版本管理器(簡稱NVM)是一個簡單的bash腳本,用於管理您Linux系統上的多個活動Node.js版本。它允許您安裝多個Node.js版本,並查看所有可用於安裝的版本以及您系統中安裝的所有版本。
Nvm還支持運行特定的Node.js版本,它可以顯示安裝位置的可執行文件路徑,以及更多其他功能。
在本文中,我們將解釋如何在您的Linux發行版上安裝Node版本管理器(NVM)來管理多個活動Node.js版本。
目錄
在 Linux 中安裝 NVM(Node Version Manager)
要在您的 Linux 發行版上安裝或更新 nvm,您可以使用 curl 或 wget 命令行工具下載自動安裝腳本。
# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash OR # wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
以上的自動安裝腳本會將 nvm 存儲庫克隆到你的家目錄中的 ~/.nvm
,並將所需的源命令添加到你的shell啟動腳本,即 ~/.bash_profile、~/.zshrc、~/.profile 或 ~/.bashrc,具體取決於你所使用的 shell 程序,如以下輸出所示。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 15916 100 15916 0 0 34227 0 --:--:-- --:--:-- --:--:-- 34301 => Downloading nvm from git to '/root/.nvm' => Cloning into '/root/.nvm'... remote: Enumerating objects: 359, done. remote: Counting objects: 100% (359/359), done. remote: Compressing objects: 100% (305/305), done. remote: Total 359 (delta 40), reused 168 (delta 28), pack-reused 0 Receiving objects: 100% (359/359), 219.46 KiB | 6.86 MiB/s, done. Resolving deltas: 100% (40/40), done. * (HEAD detached at FETCH_HEAD) master => Compressing and cleaning up git repository => Appending nvm source string to /root/.bashrc => Appending bash_completion source string to /root/.bashrc => You currently have modules installed globally with `npm`. These will no => longer be linked to the active version of Node when you install a new node => with `nvm`; and they may (depending on how you construct your `$PATH`) => override the binaries of modules installed with `nvm`: ...
安裝完成後,關閉當前的終端窗口並打開一個新的終端窗口,或者簡單地源化你的shell配置文件以加載 nvm。例如,如果你使用的是bash,運行以下命令:
$ source ~/.bashrc OR $ source ~/.profile
接下來,使用以下命令驗證是否已在你的系統上安裝了 nvm。
# command -v nvm nvm
如果安裝成功,它將顯示輸出為“nvm”。
使用 NVM 安裝多個 Node.js 版本
現在是時候學習如何在Linux中使用 Node版本管理器 了。要下載、編譯和安裝最新版本的 node,運行以下命令:
# nvm install node
請注意,在上面的命令中,“node” 是最新版本的別名。
Downloading and installing node v20.0.0... Downloading https://nodejs.org/dist/v20.0.0/node-v20.0.0-linux-x64.tar.xz... ########################################################################## 100.0% Computing checksum with sha256sum Checksums matched! Now using node v20.0.0 (npm v9.6.4) Creating default alias: default -> node (-> v20.0.0)
要安裝特定的 “node” 版本,首先列出可用的 node 版本,然後按照顯示的版本進行安裝。
# nvm ls-remote # nvm install 19.0.0
Downloading and installing node v19.0.0... Downloading https://nodejs.org/dist/v19.0.0/node-v19.0.0-linux-x64.tar.xz... ########################################################################## 100.0% Computing checksum with sha256sum Checksums matched! Now using node v19.0.0 (npm v8.19.2)
一旦使用 Node.js 版本安裝了 nvm,系統現在應該指向 Node.js 和 npm 版本位於 nvm 下。您可以運行以下命令檢查所使用的 node 和 npm 執行檔或命令的位置:which 命令如下:
# which node /root/.nvm/versions/node/v20.0.0/bin/node # which npm /root/.nvm/versions/node/v20.0.0/bin/npm # node -v v20.0.0 # npm -v 9.6.4
列出已安裝的 Node.js 版本
您可以使用以下命令檢查所有已安裝的版本:
# nvm ls
-> v19.0.0 v20.0.0 system default -> node (-> v20.0.0) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v20.0.0) (default) stable -> 20.0 (-> v20.0.0) (default) lts/* -> lts/hydrogen (-> N/A) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.12 (-> N/A) lts/fermium -> v14.21.3 (-> N/A) lts/gallium -> v16.20.0 (-> N/A) lts/hydrogen -> v18.16.0 (-> N/A)
為項目選擇 Node 版本
這就是 nvm 變得有趣的地方。現在,您可以根據需求為每個正在進行的項目選擇使用的 node.js 版本。讓我們看看如何執行此操作,如下所述。
打開多個終端,並移動到 項目 目錄中。在本指南中,我們有兩個項目,位於 ~/projects/app-server
和 ~/projects/test-ui
下,它們假設需要版本 19.0.0 和 20.0.0 的節點。
------------ Project 1 ------------ $ cd ~/projects/api-server $ nvm use 19.0.0 $ node -v ------------ Project 2 ------------ $ cd ~/projects/test-ui $ nvm use 20.0.0 $ node -v
例如,如果在版本 19.0.0 活動時安裝了全局套件,當切換到版本 20.0.0 時,您需要在新版本下重新安裝全局套件。
重要的是,您可以查看特定節點版本安裝的可執行文件的路徑如下:
# nvm which 20.0.0 # nvm which 19.0.0 # nvm which system #check system-installed version of a node using “system” alias
在Linux中設置預設的Node.js版本
此外,要手動設置預設的node版本以在任何新的shell中使用,請使用別名“default”,如下所示。
# nvm alias default 20.0.0 # nvm alias default system # nvm alias default 19.0.0
.nvmrc
initialization file in your project root directory (or any parent directory) and add a node version number or any other flags or usage options that nvm understands, in it. Then use some of the commands we have just looked at above to operate with the specified version in the file.在Linux中卸載Node.js版本
要卸載或移除Node.js的版本,請執行以下命令(請勿忘記將19.0.0替換為版本號):
# nvm uninstall 19.0.0
在Linux中卸載NVM
要手動從系統中移除nvm,請運行以下命令:
# rm -rf "$NVM_DIR" OR # rm -rf ~/.nvm
然後,編輯~/.bashrc(或上面列出的適用的shell配置文件)並移除以下配置行:
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [[ -r $NVM_DIR/bash_completion ]] && \. $NVM_DIR/bash_completion
欲了解更多信息,請參見nvm --help
或前往Node版本管理器Github存儲庫:https://github.com/nvm-sh/nvm。
這就是全部!Node版本管理器是一個簡單的bash腳本,可用於在您的Linux系統上管理多個活動node.js版本。使用下面的反饋表格向我們提問或分享您的意見。
Source:
https://www.tecmint.com/nvm-install-multiple-nodejs-versions-in-linux/