Tutorial

Binary Packaging with esbuild

Bundle a Node CLI into a single file with esbuild, mark node deps external, and add a shebang so the output runs as a bin on any machine with Node.

On this page

  1. Introduction
  2. Bundle for node
  3. Mark external
  4. Add a shebang
  5. Single file
  6. Cross-platform
  7. Test the binary
  8. Sources

Introduction

This guide covers packaging a CLI binary with esbuild, a recurring concern in the cli-tooling scenario. Shipping a CLI as a folder of sources forces the user to install a dev dependency tree. Bundling into a single file with esbuild, marking the node built-ins external, and adding a shebang produces one file that runs as a bin, which installs fast and starts fast.

Bundle for node

Target node and pick CJS or ESM by your package type, so the bundle runs on Node without a transform step and keeps the startup fast.

The trade-off worth naming is that target node and pick cjs or esm by your package type, so the bundle runs on node without a transform step and keeps the startup fast adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

import esbuild from 'esbuild';
await esbuild.build({
  entryPoints: ['src/cli.js'],
  bundle: true,
  platform: 'node',
  format: 'esm',
  outfile: 'dist/cli.mjs',
  banner: { js: '#!/usr/bin/env node' },
});

Mark external

Mark node built-ins and heavy runtime deps external so they resolve from the platform, which keeps the bundle small and avoids duplicate copies.

The trade-off worth naming is that mark node built-ins and heavy runtime deps external so they resolve from the platform, which keeps the bundle small and avoids duplicate copies adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

external: ['fs', 'path', 'child_process', 'yargs']

Add a shebang

Prepend a node shebang and set the file executable so the output runs as a bin, which is what makes it a CLI rather than a module.

The trade-off worth naming is that prepend a node shebang and set the file executable so the output runs as a bin, which is what makes it a cli rather than a module adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Single file

Bundle into one file so install is a copy, not a tree of sources, which is faster and avoids version drift between the CLI files.

The trade-off worth naming is that bundle into one file so install is a copy, not a tree of sources, which is faster and avoids version drift between the cli files adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Cross-platform

Use path.posix or handle line endings so the binary runs on Windows too, which is the common gap in a shebang script.

The trade-off worth naming is that use path.posix or handle line endings so the binary runs on windows too, which is the common gap in a shebang script adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Test the binary

Run the bundled file with argv and assert the same output as the source, so the bundle did not drop a dependency or change behavior.

The trade-off worth naming is that run the bundled file with argv and assert the same output as the source, so the bundle did not drop a dependency or change behavior adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.

Sources

Sources: esbuild, Node shebang.

FAQ

Bundle or publish source?
Bundle for a CLI so install is fast and there is one file; publish source for a library so the consumer bundler can tree-shake, which is the opposite need.
What to mark external?
Node built-ins like fs and child_process, and any runtime dependency that is better left to the platform, so the bundle stays small and works across Node versions.

Related Examples