在 Google Cloud VM 上架設你自己的套件管理伺服器 (npm library server)

By PH

50

35

Verdaccio Node.js Npm

本文介紹如何在 Google Cloud VM 上設置私有的 npm 套件管理伺服器,包括啟用服務、建立 VM、安裝必要軟體(如 Node.js、Git、Nginx)以及使用 Verdaccio 建立私有 npm 倉庫。

  • 啟用服務並建立 VM:
    • 在 Google Cloud 中啟用 Compute Engine API,並建立一台用於安裝 npm 套件庫的 VM。
    • 設定 VM 的名稱、區域、可用區,以及機器規格。
    • 設定網路流量,允許 HTTP 存取。
  • 透過 SSH 連線至 VM:
    • 在 VM 的中繼資料中設定 SSH 公鑰。
    • 使用終端機透過 SSH 公鑰連線至 VM。
  • 建置 VM 環境:
    • 更新 Linux 系統:sudo apt update
    • 安裝必要工具:curlnode.js(版本 18)、gitnginx
    • 檢查 node.js 是否安裝成功:node -v
  • 安裝並設定 Verdaccio:
    • 在 VM 中建立一個新的 Node.js 專案目錄,並初始化專案。
    • 安裝 Verdaccio:npm i verdaccio
    • 建立 index.js,引入並啟動 Verdaccio 伺服器,設定監聽埠為 4873。
    • 修改 package.jsonscripts,新增啟動指令:"server": "node index.js"
  • 設定反向代理:
    • 編輯 Nginx 配置檔,設定反向代理,將特定路徑的請求轉發至 Verdaccio 伺服器。
    • 重新載入 Nginx 配置:sudo systemctl reload nginx
  • 新增 npm 倉庫使用者:
    • 使用 npm adduser --registry http://localhost:4873 新增使用者。
  • 透過 IP 存取 Verdaccio 網頁介面:
    • 在瀏覽器中輸入 VM 的 IP 地址,即可存取 Verdaccio 的網頁介面。

參考來源: 在 Google Cloud VM 上架設 npm 套件管理伺服器