pkg打包

pkg打包

四月 18, 2022

pkg打包

参考资料

  1. https://github.com/vercel/pkg
  2. https://github.com/vercel/pkg/issues/782
  3. https://www.mianshigee.com/project/pkg
  4. https://jingsam.github.io/2018/03/02/pkg.html

简介

pkg能够将 Node.js项目打包成可执行文件,是项目能够在即使未安装 Node.js的设备上也可以运行。

1
2
3
npm install pkg --save-dev
# 或者
npm install pkg -g

详细配置

通过命令行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
pkg [options] <input>
Options:

-h, --help output usage information
帮助
-v, --version output pkg version
pkg版本
-t, --targets comma-separated list of targets (see examples)
目标系统、node版本列表
-c, --config package.json or any json file with top-level config
配置json文件
--options bake v8 options into executable to run with them on
node或v8运行时选项,例如 max-old-space-size=
参考 https://nodejs.org/api/cli.html
https://www.nodeapp.cn/cli.html
-o, --output output file name or template for several files
输出文件名称
--out-path path to save output one or more executables
输出文件目录
-d, --debug show more information during packaging process [off]
输出debug日志
非常有用,解决问题基本全靠它
-b, --build don't download prebuilt base binaries, build them
从源代码编译基本二进制文件,而非下载好的编译版本,当前系统需要满足编译要求
--public speed up and disclose the sources of top-level project
加快顶级项目来源公开
--public-packages force specified packages to be considered public
强制指定的包被认为是公共的
--no-bytecode skip bytecode generation and include source files as plain js
跳过字节码生成并将源文件包含为普通js
--no-native-build skip native addons build
跳过本地插件的构建
--no-dict comma-separated list of packages names to ignore dictionaries. Use --no-dict * to disable all dictionaries
以逗号分隔的包名列表,以忽略字典。使用——no-dict *禁用所有字典
-C, --compress [default=None] compression algorithm = Brotli or GZip
压缩算法

Examples:
举例
– Makes executables for Linux, macOS and Windows
$ pkg index.js
– Takes package.json from cwd and follows 'bin' entry
$ pkg .
– Makes executable for particular target machine
$ pkg -t node14-win-arm64 index.js
– Makes executables for target machines of your choice
$ pkg -t node12-linux,node14-linux,node14-win index.js
– Bakes '--expose-gc' and '--max-heap-size=34' into executable
$ pkg --options "expose-gc,max-heap-size=34" index.js
– Consider packageA and packageB to be public
$ pkg --public-packages "packageA,packageB" index.js
– Consider all packages to be public
$ pkg --public-packages "*" index.js
– Bakes '--expose-gc' into executable
$ pkg --options expose-gc index.js
– reduce size of the data packed inside the executable with GZip
$ pkg --compress GZip index.js

通过配置文件

1
2
3
pkg .
# 或者
pkg package.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 配置文件package.json
{
"name": "MisakiAkeno",
"version": "1.0.0",
"author": "TaiToNekoyuu",
// 入口文件
"bin": "index.js",
"pkg": {
"scripts": ["./src/*.js", "./index.js"],
"targets": ["node14-win-x64"],
"assets": [
"./node_modules/**/*"
],
// 支持esm
"options": ["experimental-modules"],
"outputPath": "dist"
}
}

可能遇到的问题及解决方法

  1. 打包失败,部分依赖可能会无法打包,可以将以来作为assets打包
  2. commonjsesm语法问题
    • pkg只支持commonjs语法,如果依赖中存在esm语法将无法运行,这是只能将原有依赖的esm语法文件主动转化为commonjs语法文件

其他链接

  1. https://nodejs.org/api/cli.html
  2. https://www.nodeapp.cn/cli.html