Node.js v18.20.8 文档


单个可执行应用#>

【Single executable applications】

稳定性: 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 二进制文件注入 JavaScript 文件来创建 单个可执行应用。在启动过程中,程序会检查是否有任何文件被注入。如果找到了脚本,它会执行其中的内容。否则,Node.js 会照常运行。

【Node.js supports the creation of single executable applications by allowing the injection of a JavaScript file into the node binary. During start up, the program checks if anything has been injected. If the script is found, it executes its contents. Otherwise Node.js operates as it normally does.】

单一可执行应用功能仅支持运行单个嵌入的 CommonJS 文件。

【The single executable application feature only supports running a single embedded CommonJS file.】

一个打包的 JavaScript 文件可以通过任何可以向 node 二进制文件注入资源的工具转换成一个单一的可执行应用。

【A bundled JavaScript file can be turned into a single executable application with any tool which can inject resources into the node binary.】

以下是使用其中一个工具 后投 创建单个可执行应用的步骤:

【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. 创建一个 node 可执行文件的副本,并根据你的需要命名:

    $ cp $(command -v node) hello 
  3. 移除二进制文件的签名:

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

    签名工具 可以从已安装的 Windows 软件开发工具包 使用。如果跳过此步骤,请忽略来自 postject 的任何与签名相关的警告。

    $ signtool remove /s hello 
  4. 通过使用以下选项运行 postject 将 JavaScript 文件注入复制的二进制文件:

    • hello - 在第 2 步中创建的 node 可执行文件副本的名称。
    • NODE_JS_CODE - 二进制文件中存放 JavaScript 文件内容的资源/注释/部分的名称。
    • hello.js - 在步骤 1 中创建的 JavaScript 文件的名称。
    • --sentinel-fuse NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2 - Node.js 项目用来检测文件是否被注入的 保险丝
    • --macho-segment-name NODE_JS(仅在 macOS 上需要)- 二进制文件中存储 JavaScript 文件内容的段的名称。

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

    • 在 macOS 以外的系统上:

      $ npx postject hello NODE_JS_CODE hello.js \
          --sentinel-fuse NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2 
    • 在 macOS 上:

      $ npx postject hello NODE_JS_CODE hello.js \
          --sentinel-fuse NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2 \
          --macho-segment-name NODE_JS 
  5. 签署二进制文件:

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

    要使其工作,需要提供证书。然而,未签名的二进制文件仍然可以运行。

    $ signtool sign /fd SHA256 hello 
  6. 运行二进制文件:

    $ ./hello world
    Hello, world! 

注意事项#>

【Notes】

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

require(id) in the injected module is not file based】

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

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

【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 在注入模块中#>

__filename and module.filename in the injected module】

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

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

注入模块中的 __dirname#>

__dirname in the injected module】

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

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

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

【Single executable application creation process】

一个旨在创建单一可执行 Node.js 应用的工具必须将 JavaScript 文件的内容注入到:

【A tool aiming to create a single executable Node.js application must inject the contents of a JavaScript file into:】

  • 如果 node 可执行文件是 体育 文件,则名为 NODE_JS_CODE 的资源
  • 如果 node 二进制文件是 Mach-O 文件,则在 NODE_JS 段中名为 NODE_JS_CODE 的部分
  • 如果 node 可执行文件是 精灵 文件,则名为 NODE_JS_CODE 的注意

在二进制文件中搜索 NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2:0 保险丝 字符串,并将最后一个字符改为 1,以表示资源已被注入。

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

平台支持#>

【Platform support】

单一可执行文件支持仅在以下平台上通过 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 发起讨论,以帮助我们记录它们。

Node.js 中文网 - 粤ICP备13048890号