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

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:

  1. Removing or renaming a command, subcommand or flag.
  2. Changing a flag's default value.
  3. Changing exit codes, since CI pipelines branch on them.
  4. Changing machine-readable output, such as JSON shape or column order in a --format mode.
  5. 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

Related pages