Node.js JXcore 打包
JXcore 是什么、为什么现在不用它、今天该怎么打包 Node 应用。
Key PointsJXcore 早已停止维护;今天打包 Node 应用用 `pkg`、`nexe` 或 Node SEA。
JXcore 是什么
JXcore was a Node.js fork around 2014 featuring multithreading (jxcore.tasks) and packaging scripts into a single executable (jx package). It saw some IoT and embedded use, but development stopped in 2017 and the site is gone.
Bash
# 历史命令,仅作了解 —— 现在跑不通
jx package app.js appOutput
# JXcore 已停止维护,此命令在当前环境不可用
# 现代替代方案见下文This chapter exists because many older tutorials still mention JXcore. Seeing
jx package tells you the material predates 2015 — do not follow it.今天的三种打包方式
| Tool | Output | Note |
|---|---|---|
| Node SEA | 单个可执行文件 | 官方方案,Node 20+ 实验性 |
pkg | 单文件 exe | 最成熟,支持多平台交叉编译 |
nexe | 单文件 exe | 从源码编译 Node |
electron-builder | 安装包 | 桌面应用场景 |
用 pkg
Bash
npm i -g @yao-pkg/pkg
pkg app.js --targets node22-win-x64 --output app.exeJSON
{
"bin": "app.js",
"pkg": {
"assets": ["views/**/*", "public/**/*"],
"scripts": ["lib/**/*.js"]
}
}pkg bundles the Node runtime with your code into one exe. Two caveats: dynamically required paths are not detected — list them in assets; and **native .node modules must be rebuilt per target platform**.
用 Node SEA
Bash
# 1. 写配置
cat > sea-config.json <<'EOF'
{
"main": "app.js",
"output": "sea-prep.blob",
"disableExperimentalSEAWarning": true
}
EOF
# 2. 生成 blob
node --experimental-sea-config sea-config.json
# 3. 复制 node 可执行文件并注入
cp $(command -v node) app
npx postject app NODE_SEA_BLOB sea-prep.blob \
--sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2要不要打包
- 要 —— 交付给不懂技术的用户、内网无 Node 环境、需要隐藏源码
- 不要 —— 服务器部署。直接
node app.js+ 进程管理器更简单,也更好排错
JXcore 是什么
JXcore 是 2014 年前后一个 Node.js 的分支,主打两个功能:多线程(jxcore.tasks)和把脚本打包成单个可执行文件(jx package)。它一度被用在 IoT 和嵌入式场景。项目在 2017 年停止维护,官网已下线。
Bash
# 历史命令,仅作了解 —— 现在跑不通
jx package app.js appOutput
# JXcore 已停止维护,此命令在当前环境不可用
# 现代替代方案见下文这一章保留是因为很多老教程还在讲 JXcore。看到
jx package 就知道那是 2015 年前的内容,不要照着做。今天的三种打包方式
| 工具 | 产物 | 说明 |
|---|---|---|
| Node SEA | 单个可执行文件 | 官方方案,Node 20+ 实验性 |
pkg | 单文件 exe | 最成熟,支持多平台交叉编译 |
nexe | 单文件 exe | 从源码编译 Node |
electron-builder | 安装包 | 桌面应用场景 |
用 pkg
Bash
npm i -g @yao-pkg/pkg
pkg app.js --targets node22-win-x64 --output app.exeJSON
{
"bin": "app.js",
"pkg": {
"assets": ["views/**/*", "public/**/*"],
"scripts": ["lib/**/*.js"]
}
}pkg 会把 Node 运行时和你的代码打进一个 exe。注意两点:**动态 require 的路径它分析不到**,要写进 assets;**原生模块(.node)需要针对目标平台重新编译**。
用 Node SEA
Bash
# 1. 写配置
cat > sea-config.json <<'EOF'
{
"main": "app.js",
"output": "sea-prep.blob",
"disableExperimentalSEAWarning": true
}
EOF
# 2. 生成 blob
node --experimental-sea-config sea-config.json
# 3. 复制 node 可执行文件并注入
cp $(command -v node) app
npx postject app NODE_SEA_BLOB sea-prep.blob \
--sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2要不要打包
- 要 —— 交付给不懂技术的用户、内网无 Node 环境、需要隐藏源码
- 不要 —— 服务器部署。直接
node app.js+ 进程管理器更简单,也更好排错