单个可执行应用


【Single executable applications】

稳定性: 1.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 来创建 单个可执行应用,该 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.】

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

【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. 生成要注入的数据块:

    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 上(可选):

    签名工具 可以从已安装的 Windows 软件开发工具包 使用。如果跳过此步骤,请忽略来自 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 项目用来检测文件是否被注入的 保险丝
    • --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!