单个可执行应用
¥Single executable applications
¥Stability: 1.1 - Active development
源代码: 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:
-
创建一个 JavaScript 文件:
¥Create a JavaScript file:
echo 'console.log(`Hello, ${process.argv[2]}!`);' > hello.js
-
创建一个配置文件,构建一个可以注入单个可执行应用的 blob(有关详细信息,请参阅 生成单个可执行准备 blob):
¥Create a configuration file building a blob that can be injected into the single executable application (see Generating single executable preparation blobs for details):
echo '{ "main": "hello.js", "output": "sea-prep.blob" }' > sea-config.json
-
生成要注入的 blob:
¥Generate the blob to be injected:
node --experimental-sea-config sea-config.json
-
创建
node
可执行文件的副本并根据需要命名:¥Create a copy of the
node
executable and name it according to your needs:-
在 Windows 以外的系统上:
¥On systems other than Windows:
cp $(command -v node) hello
-
在 Windows 上:
¥On Windows:
node -e "require('fs').copyFileSync(process.execPath, 'hello.exe')"
.exe
扩展名是必需的。¥The
.exe
extension is necessary. -
-
删除二进制文件的签名(仅限 macOS 和 Windows):
¥Remove the signature of the binary (macOS and Windows only):
-
在 macOS 上:
¥On macOS:
codesign --remove-signature hello
-
在 Windows 上(可选):
¥On Windows (optional):
signtool 可以从已安装的 Windows SDK 使用。如果跳过此步骤,请忽略来自 postject 的任何与签名相关的警告。
¥signtool can be used from the installed Windows SDK. If this step is skipped, ignore any signature-related warning from postject.
signtool remove /s hello.exe
-
-
通过使用以下选项运行
postject
将 blob 注入到复制的二进制文件中:¥Inject the blob into the copied binary by running
postject
with the following options:-
hello
/hello.exe
- 在步骤 4 中创建的node
可执行文件副本的名称。¥
hello
/hello.exe
- The name of the copy of thenode
executable created in step 4. -
NODE_SEA_BLOB
- 二进制文件中将存储 blob 内容的资源/注释/部分的名称。¥
NODE_SEA_BLOB
- The name of the resource / note / section in the binary where the contents of the blob will be stored. -
sea-prep.blob
- 在步骤 1 中创建的 blob 的名称。¥
sea-prep.blob
- The name of the blob created in step 1. -
--sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2
- Node.js 项目用来检测文件是否被注入的 fuse。¥
--sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2
- The fuse used by the Node.js project to detect if a file has been injected. -
--macho-segment-name NODE_SEA
(仅在 macOS 上需要) - 二进制文件中将存储 blob 内容的段的名称。¥
--macho-segment-name NODE_SEA
(only needed on macOS) - The name of the segment in the binary where the contents of the blob will be stored.
总而言之,这是每个平台所需的命令:
¥To summarize, here is the required command for each platform:
-
在 Linux 上:
¥On Linux:
npx postject hello NODE_SEA_BLOB sea-prep.blob \ --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2
-
在 Windows 上 - 电源外壳:
¥On Windows - PowerShell:
npx postject hello.exe NODE_SEA_BLOB sea-prep.blob ` --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2
-
在 Windows 上 - 命令提示符:
¥On Windows - Command Prompt:
npx postject hello.exe NODE_SEA_BLOB sea-prep.blob ^ --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2
-
在 macOS 上:
¥On macOS:
npx postject hello NODE_SEA_BLOB sea-prep.blob \ --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 \ --macho-segment-name NODE_SEA
-
-
签署二进制文件(仅限 macOS 和 Windows):
¥Sign the binary (macOS and Windows only):
-
在 macOS 上:
¥On macOS:
codesign --sign - hello
-
在 Windows 上(可选):
¥On Windows (optional):
需要有证书才能工作。但是,未签名的二进制文件仍然可以运行。
¥A certificate needs to be present for this to work. However, the unsigned binary would still be runnable.
signtool sign /fd SHA256 hello.exe
-
-
运行二进制文件:
¥Run the binary:
-
在 Windows 以外的系统上
¥On systems other than Windows
$ ./hello world Hello, world!
-
在 Windows 上
¥On Windows
$ .\hello.exe world Hello, world!
-