On this page
Single executable applications
Source Code: src/node_sea.cc
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 copyCreate a copy of the
nodeexecutable and name it according to your needs:$ cp $(command -v node) hello copyInject the JavaScript file into the copied binary by running
postjectwith the following options:hello- The name of the copy of thenodeexecutable 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 copyOn macOS:
$ npx postject hello NODE_JS_CODE hello.js \ --sentinel-fuse NODE_JS_FUSE_fce680ab2cc467b6e072b8b5df1996b2 \ --macho-segment-name NODE_JS copy
Run the binary:
$ ./hello world Hello, world! copy
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); copy
__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_CODEif thenodebinary is a PE file - a section named
NODE_JS_CODEin theNODE_JSsegment if thenodebinary is a Mach-O file - a note named
NODE_JS_CODEif thenodebinary 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.
© Joyent, Inc. and other Node contributors
Licensed under the MIT License.
Node.js is a trademark of Joyent, Inc. and is used with its permission.
We are not endorsed by or affiliated with Joyent.
https://nodejs.org/dist/latest-v18.x/docs/api/single-executable-applications.html