Publishing a Command-Line Tool as a Package
A library exposes functions; a CLI exposes commands, flags and output. Registries handle both, but a command-line package has its own manifest entries and its own idea of what "breaking" means. This note covers the wiring in the three ecosystems people ask about most, and the compatibility promises that are easy to forget.
Declaring the executable
npm. The bin field maps a command name to a file. On install, npm links that file onto the user's path (or into node_modules/.bin for local installs).
{
"name": "tidy-json",
"version": "1.0.0",
"bin": { "tidy-json": "./bin/cli.js" },
"files": ["bin", "dist"],
"engines": { "node": ">=20" }
}
The file needs a shebang as its very first line, #!/usr/bin/env node; npm takes care of marking the linked file executable. Forgetting the shebang is the classic cause of a command that works through npx on one machine and fails on another.
Python. Console entry points are declared in pyproject.toml; the installer generates a small launcher script for each, so no shebang is needed in your source.
[project.scripts]
tidy-json = "tidy_json.cli:main"
Go. There is no manifest entry: any package main becomes a binary. Users install it with go install example.com/you/tidy-json/cmd/tidy-json@latest, and the command name comes from the last element of the path. Putting commands under cmd/ keeps them apart from importable packages.
How users will run it
- One-off runners such as
npxandpipx rundownload and execute without a permanent install. Expect people to run your CLI this way first, so keep install-time weight low. - Isolated installs via
pipx installor a global npm install keep your dependencies away from the user's projects. - Project-local devDependencies are common for build tools; then your version is pinned by their lockfile and invoked from scripts.
Test all three paths. A missing file in the published tarball only shows up when someone installs from the registry, which is why testing the packed tarball matters even more for CLIs.
What is a breaking change for a command?
For a library, semver protects the import surface. For a CLI, the surface is everything a script might depend on. Treat each of these as a major-version change:
- Removing or renaming a command, subcommand or flag.
- Changing a flag's default value.
- Changing exit codes, since CI pipelines branch on them.
- Changing machine-readable output, such as JSON shape or column order in a
--formatmode. - Raising the minimum runtime version.
Human-readable messages, colours and help text are usually fair game in a minor release, provided there is a stable machine-readable mode for scripts to use instead. Say so in the README so users know which output to parse.
A CLI pre-publish checklist
--helpand--versionboth work and the version matches the manifest.- The command runs from a fresh install on each supported OS, not just from your working copy.
- Errors go to stderr and results to stdout, so piping behaves.
- Non-zero exit codes are documented.
- If the package is also a library, the CLI's dependencies do not bloat installs for library-only users; consider an extra or a separate package, as discussed in optional dependencies and extras.