Nodejs使用pkg打包为可执行文件

安装pkg

js 复制代码
npm install -g pkg

查看pkg命令

js 复制代码
pkg --help

修改package.json

新增bin入口配置

js 复制代码
{
  "name": "takescreenshot",
  "version": "1.0.0",
  "bin": "app.js",   // 新增bin入口配置
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "express": "^4.21.1",
  }
}

打包

默认打包

js 复制代码
pkg .

如果上面package.json没有配置bin的话,可以执行

js 复制代码
pkg .\app.js

指定打包的文件名

js 复制代码
pkg . --target win --output 你的文件名.exe

报错解决

js 复制代码
PS E:\TakeProject\takeScreenShot> pkg .        
> pkg@5.8.1
> Targets not specified. Assuming:
  node16-linux-x64, node16-macos-x64, node16-win-x64
> Fetching base Node.js binaries to PKG_CACHE_PATH
  fetched-v16.16.0-win-x64            [                    ] 0%> Not found in remote cache:
  {"tag":"v3.4","name":"node-v16.16.0-win-x64"}
> Building base binary from source:
  built-v16.16.0-win-x64
> Fetching Node.js source archive from nodejs.org...
> Error! AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

  (0, assert_1.default)(!this.bar)

    at Log.enableProgress (D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\log.js:64:30)
    at D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\utils.js:58:31
    at step (D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\utils.js:33:23)
    at Object.next (D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\utils.js:14:53)
    at D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\utils.js:8:71
    at new Promise (<anonymous>)
    at __awaiter (D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\utils.js:4:12)
    at downloadUrl (D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\utils.js:53:12)
    at D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\build.js:142:66
    at step (D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\build.js:44:23)

进入网站: https://github.com/vercel/pkg-fetch/releases

找到我报错的tag的版本,点进这个版本的详情


找到报错的对象的name值里面的版本号信息

例如上面我的报错的版本是16.16.0,那就在网站上面找到这个版本


下载报错的name的名称对应文件

我报错的是{"tag":"v3.4","name":"node-v16.16.0-win-x64"}

则在这里找到node-v16.16.0-win-x64点击下载

下面的v3.4是上面报错的对象的tag属性的值

找到C:\Users\22560\.pkg-cache\v3.4文件夹

将上面下载的文件放到文件夹内,并重命名为fetched-v16.16.0-win-x64

同理:linux和macos下载也是一样,复制到该文件夹内然后将前缀node改成fetched即可

然后重新pkg .即可

相关推荐
lpfasd1231 天前
Ollama 还是 LM Studio:在 24GB 的 M6 Mac 上,我把 Ollama 卸载了
macos·策略模式
福大大架构师每日一题3 天前
ollama v0.40.11发布:macOS原生语音生成上线,d3决策模型正式加入
macos·策略模式
AIex-YH6 天前
发酵罐里点一盏灯:光发酵,正在重算微藻蛋白的成本账
运维·制造·策略模式
snakecy6 天前
macOS 安装 LaTeX 全流程指南:从 MacTeX 到中文排版
macos·策略模式
钱栈up11 天前
从 macOS 切回 Windows,我用 5 个免费工具把 80% 操作习惯搬了回来
windows·macos·策略模式
kunicoding13 天前
C++ 策略模式实战:写一个可以“一键换输出“的日志系统
开发语言·c++·策略模式
峥嵘life14 天前
Android16 开机后立即打开相机闪退问题分析与解决总结
android·开发语言·数码相机·策略模式
智慧城市203015 天前
关键供应商连续交付不合格批次,说明质量风险可能持续存在。企业需要建立“批次隔离、加严检验、驻厂遏制、复检放行、供应商升级”的处置闭环,同时统筹保供。
数据分析·策略模式·战略咨询·流程管理
happy_baymax16 天前
Dell G3 3590 黑苹果实战:OpenCore 1.0.8 + NullMoth 驱动让 GTX 1660 跑起 Metal
策略模式
遇码16 天前
macOS 触摸板捏合在 GPUI 里无响应:vendor 打补丁,把手势合成为 cmd+滚轮事件
rust·图形渲染·策略模式·桌面应用·gpui