- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Corepack
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:module
API - Modules: Packages
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
Node.js v20.0.0-nightly202303010597f1b673 documentation
- Node.js v20.0.0-nightly202303010597f1b673
- ► Table of contents
-
►
Index
- Assertion testing
- Asynchronous context tracking
- Async hooks
- Buffer
- C++ addons
- C/C++ addons with Node-API
- C++ embedder API
- Child processes
- Cluster
- Command-line options
- Console
- Corepack
- Crypto
- Debugger
- Deprecated APIs
- Diagnostics Channel
- DNS
- Domain
- Errors
- Events
- File system
- Globals
- HTTP
- HTTP/2
- HTTPS
- Inspector
- Internationalization
- Modules: CommonJS modules
- Modules: ECMAScript modules
- Modules:
node:module
API - Modules: Packages
- Net
- OS
- Path
- Performance hooks
- Permissions
- Process
- Punycode
- Query strings
- Readline
- REPL
- Report
- Single executable applications
- Stream
- String decoder
- Test runner
- Timers
- TLS/SSL
- Trace events
- TTY
- UDP/datagram
- URL
- Utilities
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- Worker threads
- Zlib
- ► Other versions
- ► Options
Table of contents
Single executable applications#
Source Code: lib/internal/main/single_executable_application.js
This feature allows the distribution of a Node.js application conveniently to a system that does not have Node.js installed.
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.
The single executable application feature only supports running a single embedded CommonJS file.
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:
-
Create a JavaScript file:
$ echo 'console.log(`Hello, ${process.argv[2]}!`);' > hello.js
-
Create a copy of the
node
executable and name it according to your needs:$ cp $(command -v node) hello
-
Remove the signature of the binary:
- On macOS:
$ codesign --remove-signature hello
- On Windows (optional):
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
-
Inject the JavaScript file into the copied binary by running
postject
with the following options:hello
- The name of the copy of thenode
executable created in step 2.NODE_JS_CODE
- The name of the resource / note / section in the binary where the contents of the JavaScript file will be stored.hello.js
- The name of the JavaScript file created in step 1.--sentinel-fuse NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2
- The fuse used by the Node.js project to detect if a file has been injected.--macho-segment-name NODE_JS
(only needed on macOS) - The name of the segment in the binary where the contents of the JavaScript file will be stored.
To summarize, here is the required command for each platform:
-
On systems other than macOS:
$ npx postject hello NODE_JS_CODE hello.js \ --sentinel-fuse NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2
-
On macOS:
$ npx postject hello NODE_JS_CODE hello.js \ --sentinel-fuse NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2 \ --macho-segment-name NODE_JS
-
Sign the binary:
- On macOS:
$ codesign --sign - hello
- 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
-
Run the binary:
$ ./hello world Hello, world!
Notes#
require(id)
in the injected module is not file based#
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.
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.
However, if a file based require()
is still needed, that can also be achieved:
const { createRequire } = require('node:module');
require = createRequire(__filename);
__filename
and module.filename
in the injected module#
The values of __filename
and module.filename
in the injected module are
equal to process.execPath
.
__dirname
in the injected module#
The value of __dirname
in the injected module is equal to the directory name
of process.execPath
.
Single executable application creation process#
A tool aiming to create a single executable Node.js application must inject the contents of a JavaScript file into:
- a resource named
NODE_JS_CODE
if thenode
binary is a PE file - a section named
NODE_JS_CODE
in theNODE_JS
segment if thenode
binary is a Mach-O file - a note named
NODE_JS_CODE
if thenode
binary is an ELF file
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#
Single-executable support is tested regularly on CI only on the following platforms:
- Windows
- macOS
- Linux (AMD64 only)
This is due to a lack of better tools to generate single-executables that can be used to test this feature on other platforms.
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.