While writing the first Express backend for an asset management system, I could start it on my development machine with node app.js. What good would copying just app.js do on another machine? The entry point calls require('express'), so the destination still has to find Express.
I first searched for how to minify JS for production. Some examples were about making browser scripts and CSS smaller.1 A blog API described by Grow In Code came closer to my problem. Every update involved uploading the whole dependencies directory, containing thousands of files. The author combined the code with ncc: “After bundling, I had a single .js file that totaled 1.3mb.” That article led me to Vercel’s ncc, installed as @vercel/ncc.234
Production experience also counted when I chose ncc. As early as 2019, ZEIT, Vercel’s predecessor, was using ncc in the Node.js cloud-function builder on its Now platform. The released builder sends the user function’s entry point to ncc and collects the generated code and assets. When delivering my Express service I had essentially the same concern: would all the dependencies still be there on another machine? Knowing a cloud platform had used it for this made me more comfortable choosing it.5
If the server already has Node, I can hand over the build directory after the first step and run the entry point with the server’s Node installation. Another tutorial I read that day wanted the program to start on a machine without Node. It added a second step, packaging the Node runtime that executes JavaScript into the executable itself.6
flowchart TD
source["服务端源码和 npm 依赖"] -->|"第一步:用 ncc 或 esbuild 合并代码"| bundle["JS 入口,如 dist/index.js"]
bundle -->|"目标机已有 Node"| run["用 node dist/index.js 启动"]
bundle -->|"第二步:用 pkg 或 Node SEA 带上运行时"| exe["目标平台的可执行文件"]
exe --> direct["直接启动,如 ./service-demo"]
Does ncc minify by default? Does -m count as obfuscation?
Two tiny files illustrate the distinction. Put the following files in the same directory in a CommonJS project (the package.json can contain {"name":"service-demo","private":true,"type":"commonjs"}):
// describe.js
module.exports = function describe(serviceName) {
return serviceName + " ready";
};
// app.js
const describe = require("./describe");
console.log(describe(process.argv[2] || "service"));
Install ncc 0.38.3, the version I used, and build the same entry point three times. -o selects the output directory so the three results can be compared side by side.7
npm install --save-dev @vercel/ncc@0.38.3
npx ncc build app.js -o plain
npx ncc build app.js -m -o dist
npx ncc build app.js -m -s -C -o mapped
| Options | index.js in this example | What is in the build directory? |
|---|---|---|
| Default | 1,859 bytes | Both modules are combined in one entry. The generated loader accounts for most of the file and the function is still easy to read. |
-m (--minify) | 474 bytes | Tighter formatting and shorter local names. function describe(serviceName) becomes function describe(e). |
-m -s (also --source-map) | 543 bytes | The entry is still minified. Additional files index.js.map and sourcemap-register.js let errors in the generated code be mapped back to source locations. |
Running node plain/index.js inventory, node dist/index.js inventory and node mapped/index.js inventory printed inventory ready each time. The third build uses -C to skip the build cache. After switching flags several times I had seen ncc fail to find index.js.map. Rebuilding this way generated the map. The table measures only the entry file. The loader alone is bigger than the business logic in this tiny example, which makes the flags easy to observe. Measure your own service to see how much it shrinks.
If “obfuscation” means shorter variable names, -m does that. If the aim is to hide business logic, the recipient can still read it. For instance the minified function still contains return e+" ready". Version 0.38.3 calls Terser here, enables identifier mangling, retains function and class names, and disables Terser’s compress optimization stage. The ncc CLI has no separate obfuscation flag. The generated source map also includes sourcesContent. Giving someone that map gives them the source files recorded inside it.78
Another deployment option is -e (--external). With -e express, for example, ncc leaves Express to be loaded at runtime, so the destination machine must provide it separately. Even with defaults, native extensions such as SQLite drivers may be emitted as separate files. I therefore move the entire build directory outside the source tree, start the service and run a real database query. ncc states its design goal plainly: “Only supports Node.js programs as input / output”. Minified or otherwise, Node still interprets the entry point.9
What else can bundle server-side code?
esbuild can replace ncc for bundling JavaScript and dependencies. A custom webpack configuration can do the same. The JavaScript produced by all three can first be tested on a machine with a compatible Node version.
| Bundler | How to produce a Node entry point | Considerations |
|---|---|---|
| ncc | ncc build app.js -m -o dist | One command produced a minified entry for my Express service and collected the file assets it recognized.9 |
| esbuild | esbuild app.js --bundle --platform=node --format=cjs --outfile=build.cjs | Specify CommonJS explicitly and add --minify if needed. The DEV tutorial used this separate bundling step.106 |
| webpack | Set the entry point and target: 'node' in the configuration | Decide which dependencies to bundle and which the destination machine should load. Grow In Code mentioned this option in the ncc article.112 |
How do I deliver the runtime too?
Muhammad Bin Zafar’s DEV tutorial connects the two steps clearly. First esbuild produces build.cjs, which runs with node build.cjs. Then pkg turns it into a program that starts directly on the target platform. The ncc → pkg combination I investigated in June follows the same pattern with a different JavaScript bundler.612
| Executable builder | How it includes Node | What to verify before delivery |
|---|---|---|
| pkg | Select a target platform and architecture, and package the application and its assets alongside a matching Node runtime.13 | If the entry still references SQLite’s .node extension or reads assets at paths constructed at runtime, verify those files were collected too.913 |
| Node’s official SEA (Single Executable Applications) | Embed a prepared entry point in a copy of the Node binary.14 | The entry format, asset injection and loading of native extensions depend on the Node version.14 |
Node’s single-executable applications team maintains a list called Existing SEA Solutions. The version I read listed pkg, boxednode, nexe and node-sea side by side. Its pkg link points to yao-pkg/pkg. Vercel’s original project, used in the older DEV tutorial, has since been archived. When an old tutorial uses pkg, check which project it means.151613
For example, the Node 22.14 documentation available in 2025 described SEA support for one embedded CommonJS entry point. Its default require() can load built-in modules. To include npm dependencies, first bundle the entry with a tool such as ncc, then have Node turn it into a blob and inject it into a copy of the same Node binary. The Linux example below adapts the official procedure and reuses dist/index.js from above. The official API page changes as Node evolves. These commands follow the 22.x process.1714
printf '%s\n' '{"main":"dist/index.js","output":"app.blob"}' > sea-config.json
node --experimental-sea-config sea-config.json
node -e "require('node:fs').copyFileSync(process.execPath, 'service-demo')"
npx postject@1.0.0-alpha.6 service-demo NODE_SEA_BLOB app.blob \
--sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2
./service-demo
I moved the generated program outside the project, cleared the command search path on Linux and ran it. It still printed service ready. A database service also needs to distinguish read-only resources from files that change at runtime. The SQLite extension must load, while the database holding business data must live somewhere writable that survives upgrades. When packaging for an old Windows machine, the bundled Node runtime must itself support that machine. I had to choose Express and Node versions together for my Windows 7 batch SQL tool.
What did the asset management system actually ship?
At the end of April I added ncc build -m app.js -o dist to AssetMan’s package.json. From then on, npm run build produced a minified JS entry I could initially run with Node. In June, when I considered starting the service on machines without Node, pkg had a concrete benefit: I could keep that build command and use its output as the input for the executable.1813
On June 17 I found a tutorial connecting ncc directly to pkg. By June 26, when discussing how to integrate PM2, I explicitly wanted pkg for the production package. I had a working example to follow and could diagnose the stages independently. First run ncc’s output with Node, then see whether the executable starts the same way.12
The backend already used SQLite when I added the build script. Two lines in the entry are easy to conflate: require('sqlite3') loads the driver, while new sqlite3.Database('./database.sqlite') opens a data file relative to the process’s working directory. The Linux test package retained from late June contained index.js, 667.index.js, build/Release/node_sqlite3.node and database.sqlite under backend/dist/. The startup script changed to backend/dist/ first so the backend could find the database at the original relative path.18
To see which files ncc brings along, reduce the example to one query. With the same ncc 0.38.3 I bundled an entry point that opened an in-memory database and executed SELECT 1. The build produced two files, index.js and build/Release/node_sqlite3.node. Copying both outside the project with the directory structure intact returned 1. Copying only the JS made Node report that it could not find node_sqlite3.node while loading the driver. That explains the different jobs of the two SQLite files in the distribution: the native extension ships with the program version, while database.sqlite holds business data and must survive upgrades.
My recollection is that the onsite deployment probably used PM2 with a standalone executable. I still have the ncc script and test package locally, but no build command for the onsite binary. The combination addresses two questions: how to deliver the application, and who restarts it if the service exits. I also tried bundling PM2 itself in the JavaScript package. The errors are described in restarting a service with PM2.
References
Footnotes
-
Kellner, C. (2013, February 12). Minify Scripts/CSS in production mode with node.js. Stack Overflow. Original question ↩
-
Grow In Code. (2023, April 6). This is how to bundle a Node project correctly. Medium. Original article, first paragraph of “Why Bundle Your Project?” ↩ ↩2
-
Vercel. (n.d.). ncc. GitHub. Repository opened from Medium ↩
-
Vercel. (n.d.). @vercel/ncc. npm. Package page ↩
-
ZEIT. (2019, March 31). @now/node (0.5.2) [Node.js cloud-function builder]. npm. Released builder entry point, including
compileand the build step that calls it; Original npm release archive ↩ -
Zafar, M. B. (2022, August 20). Bundle Node.js code into single executable binary. DEV Community. Tutorial consulted, see “esbuild to bundle” and “pkg to package”. ↩ ↩2 ↩3
-
Vercel. (n.d.). ncc (v0.38.3). GitHub. Options and programmatic API defaults ↩ ↩2
-
Vercel. (n.d.). ncc compiler (v0.38.3). GitHub. Options passed to Terser ↩
-
Vercel. (n.d.). ncc (v0.38.3). GitHub. Design goals (item 3), Usage and Caveats ↩ ↩2 ↩3
-
Wallace, E. (n.d.). Getting started. esbuild documentation. Bundling for node ↩
-
webpack contributors. (n.d.). Target. webpack documentation. Node target environment ↩
-
m0_74824025. (n.d.). Packaging a Node.js project as an executable with NCC and PKG (Linux, macOS, Windows). CSDN. Page opened from search results on June 17. ↩ ↩2
-
Vercel. (n.d.). pkg. GitHub. Targets, Scripts and Assets; yao-pkg contributors. (n.d.). pkg. GitHub. Maintained fork ↩ ↩2 ↩3 ↩4
-
Node.js contributors. (n.d.). Single executable applications (v22.14.0). Node.js documentation. Creation procedure and require() in the injected main script ↩ ↩2 ↩3
-
Node.js SEA Team. (n.d.). Node.js SEA Team. GitHub. Working group repository and README consulted. ↩
-
Node.js SEA Team. (2024, October 22). Existing SEA solutions. GitHub. List consulted and version of the file at the time. ↩
-
Node.js contributors. (n.d.). Single executable applications. Node.js documentation. API documentation URL consulted. This URL changes with releases. ↩
-
CaiBorui [S2thend]. (2025, April 29). Add production build tool [commit d065cf4]. GitHub. AssetMan backend build script ↩ ↩2