Windows Node.js 環境安裝小記 (2026 版)
| | | 0 | |
關於在 Windows 管理及切換 Node.js 版本,我的的認知還停在 NVM (Node Version Manager),是時侯更新一下了。
Rust 寫的 Python 版本管理/套件安裝/虛擬環境的極速神器 uv 橫掃千軍,Node.js 版本管理上演類似劇本,Rust 開發的 Fast Node Manager (fnm) 具備比 nvm 快上數十倍的啟動與切換速度、只需單一執行檔、原生支援 Windows、依據 .nvmrc/.node-version 自動切換 Node.js 版本... 等優點,成為當前 Node.js 版本管理的首選。
在 Windows 使用 winget install Schniz.fnm 即可安裝。(註:若因網管設備憑證問題出現 0x8a15005e : The server certificate did not match any of the expected values. 錯誤,可加上 winget settings --enable BypassCertificatePinningForMicrosoftStore 略過 參考)

要在 PowerShell 使用 fnm,再在在 PowerShell Profile 檔(PS 5.1 在 %userprofile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1、PS 7+ 在 %userprofile%\Documents\PowerShell\Microsoft.PowerShell_profile.ps1)加入必要環境變數設定,找到 Microsoft.PowerShell_profile.ps1 (若不存在可自行新增),在檔案末端加上環境設定及 fnm 指令自動完成,其中的 --use-on-cd 參數可設定每次 cd 路徑時 fnm 會依據目錄下的 .nvmrc 或 .node-version 檔內容自動切換指定的 Node.js 版本 參考。至於 fnm completions --shell powershell 會在 PowerShell 環境為 fnm 指令增加參數提示與自動完成,可加可不加。
fnm env --use-on-cd --shell powershell | Out-String | Invoke-Expression
fnm completions --shell powershell | Out-String | Invoke-Expression
設定 OK 重啟 PowserShell 後用 fnm install <版號> 或 fnm install latest 安裝 Node.js,fnm use (<版號>|latest) 切換版本,用 fnm default <版號> 指定預設版本。之後便可愉快使用 Node.js 了。
至於 Node.js 的套件管理,見識過 uv 的閃電安裝及極省空間,自然無法再重回 npm 在每個專案重新下載及儲存一份 node_modules 的地獄,npm 跟新生代套件管理工具的比較如下:
| 工具 | 核心架構 | 主要優點 | 主要限制 |
|---|---|---|---|
| npm | node_modules + hoisting + cache | Node.js 生態標準、相容性高、內建於 Node.js、支援 Workspaces | 多專案可能產生重複的 node_modules |
| pnpm | Content-addressable store + hard links / symlinks | 最節省磁碟、安裝快速、嚴格依賴隔離、適合 Monorepo | 少數舊套件可能不適應嚴格的依賴結構 |
| Yarn Classic | node_modules + hoisting + cache | 成熟、既有專案廣泛使用 | 已進入維護階段 |
| Yarn Modern | PnP / node_modules 等多種 linker | PnP、Workspaces、Constraints、Plugins | PnP 對部分舊套件與工具需要額外適配 |
| Bun | 原生 Package Manager + Runtime / Toolchain | 安裝速度快、整合 Runtime、Test Runner、Bundler | 不只是套件管理器;與 Node.js 仍有相容性差異 |
現在要選,建議用 pnpm。
在 Windows 要安裝 pnmp,若是 Node.js 22.13+,建議用 npx get-pnpm 安裝,避開可能被 Windows Defender 阻擋的問題。參考
(註:npx 是 npm 的套件執行器,下載到暫存區執行完釋放空間,適用一次性工具)
小試一下,pnpm init 建立 package.json、pnpm add lodash 安裝套件、寫一段 js 引用 loadash,成功!

Comments
Be the first to post a comment