Node.js v20.10.0 文档


单个可执行应用#

稳定性: 1 - 实验: 此功能正在设计中并将更改。

源代码: src/node_sea.cc

此功能允许将 Node.js 应用方便地分发到未安装 Node.js 的系统。

This feature allows the distribution of a Node.js application conveniently to a system that does not have Node.js installed.

Node.js 通过允许将由 Node.js 准备的 blob 注入 node 二进制文件来支持 单个可执行应用 的创建,其中可以包含打包脚本。 在启动过程中,程序会检查是否注入了任何东西。 如果找到 blob,它会执行 blob 中的脚本。 否则 Node.js 会像往常一样运行。

Node.js supports the creation of single executable applications by allowing the injection of a blob prepared by Node.js, which can contain a bundled script, into the node binary. During start up, the program checks if anything has been injected. If the blob is found, it executes the script in the blob. Otherwise Node.js operates as it normally does.

单个可执行应用功能目前仅支持使用 CommonJS 模块系统运行单个嵌入式脚本。

The single executable application feature currently only supports running a single embedded script using the CommonJS module system.

用户可以使用 node 二进制文件本身和任何可以将资源注入二进制文件的工具从他们的打包脚本创建单个可执行应用。

Users can create a single executable application from their bundled script with the node binary itself and any tool which can inject resources into the binary.

以下是使用此类工具 postject 创建单个可执行应用的步骤:

Here are the steps for creating a single executable application using one such tool, postject:

  1. 创建一个 JavaScript 文件:

    echo 'console.log(`Hello, ${process.argv[2]}!`);' > hello.js 
  2. 创建一个配置文件,构建一个可以注入单个可执行应用的 blob(有关详细信息,请参阅 生成单个可执行准备 blob):

    echo '{ "main": "hello.js", "output": "sea-prep.blob" }' > sea-config.json 
  3. 生成要注入的 blob:

    node --experimental-sea-config sea-config.json 
  4. 创建 node 可执行文件的副本并根据需要命名:

    • 在 Windows 以外的系统上:
    cp $(command -v node) hello 
    • 在 Windows 上:
    node -e "require('fs').copyFileSync(process.execPath, 'hello.exe')" 

    .exe 扩展名是必需的。

  5. 删除二进制文件的签名(仅限 macOS 和 Windows):

    • 在 macOS 上:
    codesign --remove-signature hello 
    • 在 Windows 上(可选):

    signtool 可以从已安装的 Windows SDK 使用。 如果跳过此步骤,请忽略来自 postject 的任何与签名相关的警告。

    signtool remove /s hello.exe 
  6. 通过使用以下选项运行 postject 将 blob 注入到复制的二进制文件中:

    • hello / hello.exe - 在步骤 4 中创建的 node 可执行文件副本的名称。
    • NODE_SEA_BLOB - 二进制文件中将存储 blob 内容的资源/注释/部分的名称。
    • sea-prep.blob - 在步骤 1 中创建的 blob 的名称。
    • --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 - Node.js 项目用来检测文件是否被注入的 fuse
    • --macho-segment-name NODE_SEA(仅在 macOS 上需要)- 二进制文件中将存储 blob 内容的段的名称。

    总而言之,这是每个平台所需的命令:

    • 在 Linux 上:

      npx postject hello NODE_SEA_BLOB sea-prep.blob \
          --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 
    • 在 Windows 上 - PowerShell:

      npx postject hello.exe NODE_SEA_BLOB sea-prep.blob `
          --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 
    • 在 Windows 上 - 命令提示符:

      npx postject hello.exe NODE_SEA_BLOB sea-prep.blob ^
          --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 
    • 在 macOS 上:

      npx postject hello NODE_SEA_BLOB sea-prep.blob \
          --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 \
          --macho-segment-name NODE_SEA 
  7. 签署二进制文件(仅限 macOS 和 Windows):

    • 在 macOS 上:
    codesign --sign - hello 
    • 在 Windows 上(可选):

    需要有证书才能工作。 但是,未签名的二进制文件仍然可以运行。

    signtool sign /fd SHA256 hello.exe 
  8. 运行二进制文件:

    • 在 Windows 以外的系统上
    $ ./hello world
    Hello, world! 
    • 在 Windows 上
    $ .\hello.exe world
    Hello, world! 

生成单个可执行准备 blob#

可以使用将用于构建单个可执行文件的 Node.js 二进制文件的 --experimental-sea-config 标志生成注入到应用中的单个可执行文件准备 blob。 它采用 JSON 格式的配置文件路径。 如果传递给它的路径不是绝对路径,Node.js 将使用相对于当前工作目录的路径。

Single executable preparation blobs that are injected into the application can be generated using the --experimental-sea-config flag of the Node.js binary that will be used to build the single executable. It takes a path to a configuration file in JSON format. If the path passed to it isn't absolute, Node.js will use the path relative to the current working directory.

该配置当前读取以下顶层字段:

The configuration currently reads the following top-level fields:

{
  "main": "/path/to/bundled/script.js",
  "output": "/path/to/write/the/generated/blob.blob",
  "disableExperimentalSEAWarning": true, // Default: false
  "useSnapshot": false,  // Default: false
  "useCodeCache": true // Default: false
} 

如果路径不是绝对路径,Node.js 将使用相对于当前工作目录的路径。 用于生成 blob 的 Node.js 二进制文件的版本必须与将注入 blob 的版本相同。

If the paths are not absolute, Node.js will use the path relative to the current working directory. The version of the Node.js binary used to produce the blob must be the same as the one to which the blob will be injected.

启动快照支持#

useSnapshot 字段可用于启用启动快照支持。 在这种情况下,启动最终可执行文件时将不会出现 main 脚本。 相反,它将在构建计算机上生成单个可执行应用准备 blob 时运行。 然后,生成的准备 blob 将包含捕获由 main 脚本初始化的状态的快照。 注入准备 blob 的最终可执行文件将在运行时反序列化快照。

The useSnapshot field can be used to enable startup snapshot support. In this case the main script would not be when the final executable is launched. Instead, it would be run when the single executable application preparation blob is generated on the building machine. The generated preparation blob would then include a snapshot capturing the states initialized by the main script. The final executable with the preparation blob injected would deserialize the snapshot at run time.

useSnapshot 为 true 时,主脚本必须调用 v8.startupSnapshot.setDeserializeMainFunction() API 来配置用户启动最终可执行文件时需要运行的代码。

When useSnapshot is true, the main script must invoke the v8.startupSnapshot.setDeserializeMainFunction() API to configure code that needs to be run when the final executable is launched by the users.

应用在单个可执行应用中使用快照的典型模式是:

The typical pattern for an application to use snapshot in a single executable application is:

  1. 在构建时,在构建机器上,运行主脚本以将堆初始化为准备接受用户输入的状态。 该脚本还应该使用 v8.startupSnapshot.setDeserializeMainFunction() 配置一个 main 函数。 该函数将被编译并序列化到快照中,但不会在构建时调用。
  2. 在运行时,主函数将在用户计算机上的反序列化堆之上运行,以处理用户输入并生成输出。

当用于为单个可执行应用构建快照时,启动快照脚本的一般约束也适用于主脚本,并且主脚本可以使用 v8.startupSnapshot API 来适应这些约束。 参见 有关 Node.js 中启动快照支持的文档

The general constraints of the startup snapshot scripts also apply to the main script when it's used to build snapshot for the single executable application, and the main script can use the v8.startupSnapshot API to adapt to these constraints. See documentation about startup snapshot support in Node.js.

V8 代码缓存支持#

当配置中将 useCodeCache 设置为 true 时,在生成单个可执行准备 blob 期间,Node.js 将编译 main 脚本以生成 V8 代码缓存。 生成的代码缓存将成为准备 blob 的一部分,并注入到最终的可执行文件中。 当单个可执行应用启动时,Node.js 不会从头开始编译 main 脚本,而是使用代码缓存来加速编译,然后执行脚本,这将提高启动性能。

When useCodeCache is set to true in the configuration, during the generation of the single executable preparation blob, Node.js will compile the main script to generate the V8 code cache. The generated code cache would be part of the preparation blob and get injected into the final executable. When the single executable application is launched, instead of compiling the main script from scratch, Node.js would use the code cache to speed up the compilation, then execute the script, which would improve the startup performance.

useCodeCachetrue 时,注意: import() 不起作用。

Note: import() does not work when useCodeCache is true.

注意事项#

注入模块中的 require(id) 不是基于文件的#

注入模块中的 require() 与未注入模块可用的 require() 不同。 除 require.main 外,它也不具有非注入 require() 所具有的任何属性。 它只能用于加载内置模块。 尝试加载只能在文件系统中找到的模块将引发错误。

require() in the injected module is not the same as the require() available to modules that are not injected. It also does not have any of the properties that non-injected require() has except require.main. It can only be used to load built-in modules. Attempting to load a module that can only be found in the file system will throw an error.

用户可以将他们的应用打包到一个独立的 JavaScript 文件中以注入可执行文件,而不是依赖于基于 require() 的文件。 这也确保了更具确定性的依赖图。

Instead of relying on a file based require(), users can bundle their application into a standalone JavaScript file to inject into the executable. This also ensures a more deterministic dependency graph.

但是,如果仍然需要基于 require() 的文件,也可以实现:

However, if a file based require() is still needed, that can also be achieved:

const { createRequire } = require('node:module');
require = createRequire(__filename); 

注入模块中的 __filenamemodule.filename#

注入模块中的 __filenamemodule.filename 的值等于 process.execPath

The values of __filename and module.filename in the injected module are equal to process.execPath.

注入模块中的 __dirname#

注入模块中 __dirname 的值等于 process.execPath 的目录名。

The value of __dirname in the injected module is equal to the directory name of process.execPath.

单个可执行应用创建过程#

旨在创建单个可执行 Node.js 应用的工具必须将使用 --experimental-sea-config" 准备的 blob 的内容注入到:

A tool aiming to create a single executable Node.js application must inject the contents of the blob prepared with --experimental-sea-config" into:

  • 如果 node 二进制文件是 PE 文件,则名为 NODE_SEA_BLOB 的资源
  • 如果 node 二进制文件是 Mach-O 文件,则 NODE_SEA 段中名为 NODE_SEA_BLOB 的部分
  • 如果 node 二进制文件是 ELF 文件,则名为 NODE_SEA_BLOB 的注释

在二进制文件中搜索 NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2:0 fuse 字符串并将最后一个字符翻转为 1 以指示已注入资源。

Search the binary for the NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2:0 fuse string and flip the last character to 1 to indicate that a resource has been injected.

平台支持#

仅在以下平台上的 CI 上定期测试单一可执行支持:

Single-executable support is tested regularly on CI only on the following platforms:

这是由于缺乏更好的工具来生成可用于在其他平台上测试此功能的单一可执行文件。

This is due to a lack of better tools to generate single-executables that can be used to test this feature on other platforms.

欢迎对其他资源注入工具/工作流程提出建议。 请在 https://github.com/nodejs/single-executable/discussions 开始讨论以帮助我们记录它们。

Suggestions for other resource injection tools/workflows are welcomed. Please start a discussion at https://github.com/nodejs/single-executable/discussions to help us document them.