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

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

然后重新pkg .即可

相关推荐
一个在高校打杂的3 天前
honeypot之opencanary(轻量化蜜罐)
linux·网络安全·网络攻击模型·安全威胁分析·策略模式
蜡笔小马4 天前
13.C++设计模式-策略模式
c++·设计模式·策略模式
杜子不疼.5 天前
【C++ AI 大模型接入 SDK】 - LLMProvider 抽象基类与策略模式
开发语言·c++·策略模式
代码对我眨眼睛6 天前
Mac 如何单独修改鼠标滚动方向,而不影响触控板
macos·计算机外设·策略模式
jiushiaifenxiang6 天前
Parallels Desktop for Mac 26.3.2 (57398)中文版新功能介绍
macos·策略模式
雪碧聊技术7 天前
什么是策略模式?一文详解
策略模式
johnny2339 天前
终端文件管理器:Yazi、nnn、Superfile、lf、Ranger、walk
策略模式
AI砖家9 天前
DeepSeek TUI 保姆级安装配置全指南 -Windows||macOS双平台全覆盖
服务器·前端·人工智能·windows·macos·ai编程·策略模式
有梦想的小何9 天前
Cursor AI 编程实战(篇三):Domain、Infrastructure 与策略模式
java·ai编程·策略模式
多加点辣也没关系10 天前
设计模式-策略模式
java·设计模式·策略模式