Node.js 新特性 SEA/单文件可执行应用尝鲜

#1 关于 SEA

单文件可执行应用(SEA,Singe Executable Applications),是 Node.js 新版本的特性,最初在 v19.7.0、v18.16.0 加入,并在 v20.x 得到扩展。而上个月发布的全家桶 Bun.js,就自带了 SEA 功能🙃。

此前,若需要把 JS 文件打包,通常会选择下面几种工具:

名称最新版本最新发布说明
pkg5.8.12023-03-08Package your Node.js project into an executable
boxednode2.1.22023-09-27Ship a JS file with Node.js in a box
nexeV32017-08-30create a single executable out of your node.js apps
node-sea0.3.02022-09-22Pack entire packages and application into portable scripts

#2 实战 SEA

#2.1 尝鲜官方 SEA

开始前请先升级到 node v20+,否则后续的命令会报错:bad option --experimental-sea-config

首先我们写一个简单的脚本,实现在控制台用边框包裹参数文本的功能。

/**
 * @type {String}
 */
let name = process.argv[2]
if(!name || name.trim().length == 0){
    console.error("ERROR: 请输入名字")
    process.exit(-1)
}

console.log(`Hello, ${name}`)

接下来编辑 sea-config.json 文件,内容是

{
    "main": "index.js",
    "output": "index.blob"
}

执行命令node --experimental-sea-config sea-config.json后生成index.blob文件(可以用文本编辑器查看,其实就是之前的代码前加了一小段内容🙂)

接着执行:

# 将 node 环境包装成 exe 可执行文件(大小约为 68M)
node -e "require('fs').copyFileSync(process.execPath, 'hello.exe')" 
# 此时运行 ./hello.exe 与执行 node 命令效果一致😂

# 接下来就是把 blob 文件转载到 hello.exe 内
npx postject hello.exe NODE_SEA_BLOB index.blob --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 --overwrite
# 首次执行时,会提示安装 postject 包;增加 --overwrite 强制覆盖


运行效果

#2.2 PKG方案

# 请先安装 pkg:npm install -g pkg

# 执行打包
pkg -t node16-win-x64 index.js -o hello-pkg.exe
# 大概 2 秒即可完成,生成 34M 大小的可执行文件
# 注意:按照官方文档,目前 pkg 最新支持到 node19 的打包,详见 https://github.com/vercel/pkg-fetch/blob/main/patches/patches.json

#2.3 存在的问题

SEA 本质就是把 node 环境与我们自己的 JS 文件一同打包,所以得到的产物体积都偏大。实际情况下,个人首选 pkg。

另外,官方及 pkg 都只支持单文件打包,若存在第三方库、文件的依赖,在运行时就会报 module 找不到的异常。

此时,可以使用诸如 webpack、vite 等构建工具把项目编译成单文件,再进行 SEA 包装😄。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
ERROR Failed to compile with 48 errors 上午10:53:54 These dependencies were not found: * core-js/modules/es.array.push.js in ./node_modules/.store/@[email protected]/node_modules/@babel/runtime/helpers/esm/objectSpread2.js, ./node_modules/.store/[email protected]/node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/.store/[email protected]/node_modules/babel-loader/lib!./node_modules/.store/[email protected]/node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/.store/[email protected]/node_modules/vue-loader/lib??vue-loader-options!./src/components/HeaderSearch/index.vue?vue&type=script&lang=js& and 29 others * core-js/modules/es.error.cause.js in ./node_modules/.store/@[email protected]/node_modules/@babel/runtime/helpers/esm/regeneratorRuntime.js, ./node_modules/.store/[email protected]/node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/.store/[email protected]/node_modules/babel-loader/lib!./node_modules/.store/[email protected]/node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/.store/[email protected]/node_modules/vue-loader/lib??vue-loader-options!./src/layout/components/Navbar.vue?vue&type=script&lang=js& and 5 others * core-js/modules/es.object.proto.js in ./node_modules/.store/@[email protected]/node_modules/@babel/runtime/helpers/esm/regeneratorRuntime.js * core-js/modules/es.regexp.dot-all.js in ./node_modules/.store/[email protected]/node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/.store/[email protected]/node_modules/babel-loader/lib!./node_modules/.store/[email protected]/node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/.store/[email protected]/node_modules/vue-loader/lib??vue-loader-options!./src/components/ThemePicker/index.vue?vue&type=script&lang=js&, ./node_modules/.store/[email protected]/node_modules/cache-loader/dist/cjs.js??ref--12-0!./node_modules/.store/[email protected]/node_modules/babel-loader/lib!./node_modules/.store/[email protected]/node_modules/cache-loader/dist/cjs.js??ref--0-0!./node_modules/.store/[email protected]/node_modules/vue-loader/lib??vue-loader-options!./src/layout/components/Navbar.vue?vue&type=script&lang=js& and 2 others * core-js/modules/web.url-search-params.delete.js in ./src/utils/request.js * core-js/modules/web.url-search-params.has.js in ./src/utils/request.js * core-js/modules/web.url-search-params.size.js in ./src/utils/request.js * qs in ./src/utils/request.js * svg-baker-runtime/browser-symbol in ./src/icons/svg/user.svg To install them, you can run: npm install --save core-js/modules/es.array.push.js core-js/modules/es.error.cause.js core-js/modules/es.object.proto.js core-js/modules/es.regexp.dot-all.js core-js/modules/web.url-search-params.delete.js core-js/modules/web.url-search-params.has.js core-js/modules/web.url-search-params.size.js qs svg-baker-runtime/browser-symbol怎么解决如何安装
07-21

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

集成显卡

码字不易,需要您的鼓励😄

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值