#打包 #exe #pkg 使用 pkg 将 nodejs 打包编译为 exe

  • #打包#exe#pkg 使用 pkg 将 nodejs 打包编译为 exe
    • 	  # 第一步: 安装依赖
      	  $ pnpm install -g pkg
      	  
      	  # 第二步: 编写测试代码 app.js
      	  const fs = require('fs')
      	  
      	  function writeLog(message) {
      	    	console.log('20240710153205 -------> ', message)
      	  	fs.appendFileSync('alogs.txt', `${message}\n`, 'utf8')
      	  }
      	  
      	  // 示例用法
      	  writeLog('这是一条日志')
      	  
      	  # 第三步: 编写和运行 pkg 命令
      	  "scripts": {
      	      "build": "pkg -t win ./app.js --output ./dist/app.exe"
      	  }
      
      • 遇到各种问题都可以通过这篇文章找到答案: https://segmentfault.com/a/1190000041958374
        • 比较麻烦的报错如下:
          • 				  > pkg -t win ./app.js --output ./dist/app.exe
            				  
            				  > pkg@5.7.0
            				  > Fetching base Node.js binaries to PKG_CACHE_PATH
            				    fetched-v18.1.0-win-x64             [                    ] 0%> Not found in remote cache:
            				    {"tag":"v3.4","name":"node-v18.1.0-win-x64"}
            				  > Building base binary from source:
            				    built-v18.1.0-win-x64
            				  > Fetching Node.js source archive from nodejs.org...
            				  > Error! AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
            
          • 大意就是无法下载相关文件. 哪怕我 fq 也失败. 解决方法如下:
            • https://github.com/vercel/pkg-fetch/releases, 下载你需要的版本, 譬如我是 v18.1.0-win-x64, 那么就下载 node-v18.1.0-win-x64
            • 本地应当存在这个路径: C:Userslee.pkg-cachev3.4, 这个路径是 pkg 默认创建的. 将下载好的 node-v18.1.0-win-x64 放置其中, 并且重命名为 fetched-v18.1.0-win-x64
            • 重新执行编译
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值