Package your Node.js project into an executable
pkg[!IMPORTANT]
pkghas been deprecated with5.8.1as the last release. There are a number of successful forked versions ofpkgalready with various feature additions. Further, we’re excited about Node.js 21’s support for single executable applications. Thank you for the support and contributions over the years. The repository will remain open and archived.
This command line interface enables you to package your Node.js project into an executable that can be run even on devices without Node.js installed.
npm install to deploy
your application. Deploy it as a single filenpm install -g pkgAfter installing it, run pkg --help without arguments to see list of options:
…The entrypoint of your project is a mandatory CLI argument. It may be:
/path/app.js, then
packaged app will work the same way as node /path/app.jspackage.json. Pkg will follow bin property of
the specified package.json and use it as entry file.Pkg will look for package.json in
the specified directory. See above.pkg can generate executables for several target machines at a
time. You can specify a comma-separated list of targets via --targets
option. A canonical target consists of 3 elements, separated by
dashes, for example node18-macos-x64 or node14-linux-arm64:
(element) is unsupported, but you may try to compile yourself.
You may omit any element (and specify just node14 for example).
The omitted elements will be taken from current platform or
system-wide Node.js installation (its version and arch).
There is also an alias host, that means that all 3 elements
are taken from current platform/Node.js. By default targets are
linux,macos,win for current Node.js version and arch.
If you want to generate executable for different architectures,
note that by default pkg has to run the executable of the
target arch to generate bytecodes:
x64 on arm64 with Rosetta 2 but not opposite.x64 on arm64 with x64 emulation but not opposite.--no-bytecode --public-packages "*" --public.macos-arm64 is experimental. Be careful about the mandatory code signing requirement.
The final executable has to be signed (ad-hoc signature is sufficient) with codesign
utility of macOS (or ldid utility on Linux). Otherwise, the executable will be killed
by kernel and the end-user has no way to permit it to run at all. pkg tries to ad-hoc
sign the final executable. If necessary, you can replace this signature with your own
trusted Apple Developer ID.
To be able to generate executables for all supported architectures and platforms, run
pkg on a Linux host with binfmt (QEMU emulation) configured and ldid installed.
During packaging process pkg parses your sources, detects
calls to require, traverses the dependencies of your project
and includes them into executable. In most cases you
don't need to specify anything manually.
However your code may have require(variable) calls (so called non-literal
argument to require) or use non-javascript files (for
example views, css, images etc).
require('./build/' + cmd + '.js');
path.join(__dirname, 'views/' + viewName);Such cases are not handled by pkg. So you must specify the
files - scripts and assets - manually in pkg property of
your package.json file.
"pkg": {
"scripts": "build/**/*.js",
"assets": "views/**/*",
"targets": [ "node14-linux-arm64" ],
"outputPath": "dist"
}The above example will include everything in assets/ and
every .js file in build/, build only for node14-linux-arm64,
and place the executable inside dist/.
You may also specify arrays of globs:
"assets": [ "assets/**/*", "images/**/*" ]Just be sure to call pkg package.json or pkg . to make
use of package.json configuration.
scripts is a glob
or list of globs. Files specified as scripts will be compiled
using v8::ScriptCompiler and placed into executable without
sources. They must conform to the JS standards of those Node.js versions
you target (see Targets), i.e. be already transpiled.
assets is a glob
or list of globs. Files specified as assets will be packaged
into executable as raw content without modifications. Javascript
files may also be specified as assets. Their sources will
not be stripped as it improves execution performance of the
files and simplifies debugging.
See also Detecting assets in source code and Snapshot filesystem.
Node.js application can be called with runtime options
(belonging to Node.js or V8). To list them type node --help or node --v8-options.
You can "bake" these runtime options into packaged application. The app will always run with the options
turned on. Just remove -- from option name.
You can specify multiple options by joining them in a single string, comma (,) separated:
pkg app.js --options expose-gc
pkg app.js --options max_old_space_size=4096
pkg app.js --options max-old-space-size=1024,tls-min-v1.0,expose-gcYou may specify --output if you create only one executable
or --out-path to place executables for multiple targets.
Pass --debug to pkg to get a log of packaging process.
If you have issues with some particular file (seems not packaged
into executable), it may be useful to look through the log.
By default, your source code is precompiled to v8 bytecode before being written
to the output file. To disable this feature, pass --no-bytecode to pkg.
If you need a reproducible build process where your executable hashes (e.g. md5, sha1, sha256, etc.) are the same value between builds. Because compiling bytecode is not deterministic (see here or here) it results in executables with differing hashed values. Disabling bytecode compilation allows a given input to always have the same output.
While compiling to bytecode does not make your source code 100% secure, it does
add a small layer of security/privacy/obscurity to your source code. Turning
off bytecode compilation causes the raw source code to be written directly to
the executable file. If you're on *nix machine and would like an example, run
pkg with the --no-bytecode flag, and use the GNU strings tool on the
output. You then should be able to grep your source code.
Specifying --no-bytecode will fail if there are any packages in your project that aren't explicitly marked
as public by the license in their package.json.
By default, pkg will check the license of each package and make sure that stuff that isn't meant for the public will
only be included as bytecode.
If you do require building pkg binaries for other architectures and/or depend on a package with a broken
license in its package.json, you can override this behaviour by either explicitly whitelisting packages to be public
using --public-packages "packageA,packageB" or setting all packages to public using --public-packages "*"
pkg has so called "base binaries" - they are actually same
node executables but with some patches applied. They are
used as a base for every executable pkg creates. pkg
downloads precompiled base binaries before packaging your
application. If you prefer to compile base binaries from
source instead of downloading them, you may pass --build
option to pkg. First ensure your computer meets the
requirements to compile original Node.js:
BUILDING.md
See pkg-fetch for more info.
Pass --compress Brotli or --compress GZip to pkg to compress further the content of the files store in the exectable.
This option can reduce the size of the embedded file system by up to 60%.
The startup time of the application might be reduced slightly.
-C can be used as a shortcut for --compress .
| Var | Description |
|---|---|
| PKG_CACHE_PATH | Used to specify a custom path for node binaries cache folder. Default is ~/.pkg-cache |
| PKG_IGNORE_TAG | Allows to ignore additional folder created on PKG_CACHE_PATH matching pkg-fetch version |
| MAKE_JOB_COUNT | Allow configuring number of processes used for compiling |
Examples
# 1 - Using export
export PKG_CACHE_PATH=/my/cache
pkg app.js
# 2 - Passing it before the script
PKG_CACHE_PATH=/my/cache pkg app.jsCommand line call to packaged app ./app a b is equivalent
to node app.js a b
During packaging process pkg collects project files and places
them into executable. It is called a snapshot. At run time the
packaged application has access to snapshot filesystem where all
that files reside.
Packaged files have /snapshot/ prefix in their paths (or
C:\snapshot\ in Windows). If you used pkg /path/app.js command line,
then __filename value will be likely /snapshot/path/app.js
at run time. __dirname will be /snapshot/path as well. Here is
the comparison table of path-related values:
| value | with node |
packaged | comments |
|---|---|---|---|
| __filename | /project/app.js | /snapshot/project/app.js | |
| __dirname | /project | /snapshot/project | |
| process.cwd() | /project | /deploy | suppose the app is called ... |
| process.execPath | /usr/bin/nodejs | /deploy/app-x64 | app-x64 and run in /deploy |
| process.argv[0] | /usr/bin/nodejs | /deploy/app-x64 | |
| process.argv[1] | /project/app.js | /snapshot/project/app.js | |
| process.pkg.entrypoint | undefined | /snapshot/project/app.js | |
| process.pkg.defaultEntrypoint | undefined | /snapshot/project/app.js | |
| require.main.filename | /project/app.js | /snapshot/project/app.js |
Hence, in order to make use of a file collected at packaging
time (require a javascript file or serve an asset) yo
No open issues yet, or sync has not completed.