Node.js JXcore 打包

JXcore 是什么、为什么现在不用它、今天该怎么打包 Node 应用。

Intermediate
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 app
Output
# JXcore 已停止维护,此命令在当前环境不可用
# 现代替代方案见下文
This chapter exists because many older tutorials still mention JXcore. Seeing jx package tells you the material predates 2015 — do not follow it.

今天的三种打包方式

ToolOutputNote
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.exe
JSON
{
  "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 + 进程管理器更简单,也更好排错

Friends & Partners