使用 pkg 打包表格
pkg[^1] 是一种用于生成嵌入脚本的命令行工具的工具。
¥pkg[^1] is a tool for generating command-line tools that embed scripts.
SheetJS 是一个用于从电子表格读取和写入数据的 JavaScript 库。
¥SheetJS is a JavaScript library for reading and writing data from spreadsheets.
此演示使用 pkg 和 SheetJS 创建独立的 CLI 工具,用于解析电子表格并转换为其他格式。
¥This demo uses pkg and SheetJS to create a standalone CLI tool for parsing
spreadsheets and converting to other formats.
随着 NodeJS SEA 的正式发布,Vercel 选择弃用 pkg。它对于使用 NodeJS 版本 18 或更早版本部署应用仍然有用,因为这些版本不支持 NodeJS SEA。
¥With the official release of NodeJS SEA, Vercel opted
to deprecate pkg. It is still useful for deploying apps with NodeJS version 18
or earlier since those versions do not support NodeJS SEA.
该演示在以下部署中进行了测试:
¥This demo was tested in the following deployments:
| 架构 | 版本 | NodeJS | 日期 |
|---|---|---|---|
darwin-x64 | 5.8.1 | 18.5.0 | 2025-04-21 |
darwin-arm | 5.8.1 | 18.5.0 | 2025-06-18 |
win11-x64 | 5.8.1 | 18.5.0 | 2025-05-07 |
win11-arm | 5.8.1 | 18.5.0 | 2025-02-23 |
linux-x64 | 5.8.1 | 18.5.0 | 2025-04-21 |
linux-arm | 5.8.1 | 18.5.0 | 2025-02-15 |
ARM 上的 Windows 上的 pkg 使用 X64 兼容层。它不会生成原生 ARM64 二进制文件!
¥pkg on Windows on ARM uses the X64 compatibility layer. It does not generate
a native ARM64 binary!
集成详情
¥Integration Details
SheetJS NodeJS 模块 可以从脚本中需要。pkg 将自动处理打包。
¥The SheetJS NodeJS module can be
required from scripts. pkg will automatically handle packaging.
脚本要求
¥Script Requirements
专门使用 SheetJS 库和 NodeJS 内置模块的脚本可以使用 pkg 进行打包。
¥Scripts that exclusively use SheetJS libraries and NodeJS built-in modules can
be bundled using pkg.
演示脚本 xlsx-cli.js 在 NodeJS 中运行。这是一个用于读写电子表格的简单命令行工具。
¥The demo script xlsx-cli.js runs in NodeJS. It
is a simple command-line tool for reading and writing spreadsheets.
局限性
¥Limitations
上次测试此演示时,pkg 失败,并出现引用 node20 的错误:
¥When this demo was last tested, pkg failed with an error referencing node20:
> Targets not specified. Assuming:
node20-linux-arm64, node20-macos-arm64, node20-win-arm64
> Error! No available node version satisfies 'node20'
pkg 不支持 NodeJS 20、22 或 24!
¥pkg does not support NodeJS 20 or 22 or 24!
本地 NodeJS 版本必须回滚到 18 版本。
¥The local NodeJS version must be rolled back to version 18.
如果使用 nvm 或 nvm-windows 安装 NodeJS:
¥If nvm or nvm-windows was used to install NodeJS:
nvm install 18
nvm use 18
否则,在 macOS 和 Linux 上,n 可以管理全局安装:
¥Otherwise, on macOS and Linux, n can manage the global installation:
sudo npm i -g n
sudo n 18
在 Windows 上,建议使用预构建的安装程序 [^2]
¥On Windows, it is recommended to use a prebuilt installer[^2]