1. 程式人生 > 其它 >nodejs專案打包為exe

nodejs專案打包為exe

nodejs專案打包為exe

zpfei釋出於2020-04-17

需求

需要在沒有node環境的電腦上執行node程式

打包工具安裝

npminstallpkg -g

打包程式

找到程式入口檔案:pkg 入口檔案 -o 輸出名

pkg src/index.js -o app

以上指令會生成app.exe檔案

錯誤處理

  • 可能會遇到以下錯誤
> Targets not specified. Assuming:
  node10-win-x64
> Fetching base Node.js binaries to PKG_CACHE_PATH
  fetched-v10.17.0-win-x64     [                    ] 0%
> Error! getaddrinfo ENOENT github-production-release-asset-2e65be.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com:443
> Asset not found by direct link:
  {"tag":"v2.6","name":"uploaded-v2.6-node-v10.17.0-win-x64"}
> Error! read ECONNRESET
  • 處理方法
  1. 找到當前使用者home資料夾,windows一般為C:\Users\使用者名稱
  2. 在home資料夾裡找到.pkg-cache/v2.6資料夾 (具體以當前有v2.x資料夾為主)
  3. 下載對應系統對應版本(版本可相差不大)的node二進位制檔案
  4. 將第三步下載的檔案移動到第二步的資料夾(將第二步的資料夾.pkg-cache/v2.6裡的檔案先刪除完)
  5. 將移動後的檔案重新命名為fetched-v10.xx.x-win-x64(具體參考第三步下載的版本名)
  6. 重新打包

具體參考專案地址