Downloads your package from the npm registry (or uses a cached copy).
Looks for the binary you exposed in package.json → bin.
Adds the file to a temporary $PATH.
Executes it.
So the “secret sauce” is just the bin field.
jsonc
// package.json
{
"name": "my-awesome-cli",
"version": "1.2.3",
"bin": {
"my-cli": "./bin/cli.js" // <-- file that will be executed
},
"files": ["bin", "dist", "README.md"]
}
and your file itself needs the standard Node shebang:
js
#!/usr/bin/env node
console.log('Hello from my-cli!');
Publish that to npm (npm publish) and anyone can run it immediately:
bash
npx my-awesome-cli
What if the code is written in TypeScript?
There are two mainstream approaches:
1. Compile to JavaScript before publishing (most common)
• Keep your sources in src/**/*.ts.
• Transpile to dist/ (or build/) during npm publish using the prepare or prepublishOnly script.
• Point bin to the compiled JavaScript file, not the .ts file.
Example setup:
jsonc
// package.json
{
"name": "ts-awesome-cli",
"version": "1.0.0",
"scripts": {
"build": "tsc -p tsconfig.json",
"prepare": "npm run build" // runs automatically before publishing
},
"bin": {
"ts-cli": "./dist/cli.js" // compiled output
},
"files": ["dist"] // only ship JS + types, not raw TS
}
dist/cli.js (generated) retains the shebang:
js
#!/usr/bin/env node
require("../dist/index.js");
Pros
• Zero runtime dependencies (ts-node, etc.) for your users.
• Fast startup.
• Works everywhere Node works.
2. Ship raw .ts and rely on a runtime loader (less common)
Put a shebang that calls ts-node, tsx, or any ES-module runner:
ts
#!/usr/bin/env ts-node
import { main } from './index.ts';
main();
and in package.json add a dependency:
json
"dependencies": {
"ts-node": "^10.9.1"
}
Why people avoid this for public CLIs
• Extra cold-start overhead (TypeScript compilation happens at run time).
• Larger install size (you pull in typescript, ts-node).
• Doesn’t work in strict environments where ts-node is blocked.
TL;DR
Put your entry file in the bin section.
Add the #!/usr/bin/env node shebang.
Publish to npm.
For TypeScript, compile to JS first and point bin to the compiled file—this is the industry-standard approach.
User
how important is it to add "files": [] in package.json?
Assistant
Short answer
You don’t have to add a "files" array, but it is usually a very good idea.
It’s the safest, whitelisting way to control what actually ends up on npm.
Why it matters
Prevents accidental leaks
• .env, test fixtures, build cache, credentials, large datasets, etc.
• Anything not explicitly named is kept out of the tarball.
Smaller install size & faster npm i/npx
• Users only download what they need—often 10-100× smaller.
Faster publish & CI
• npm compresses fewer files; less bandwidth and storage.
Clearer intent
• Future contributors see exactly what is supposed to ship.
Overrides .gitignore/.npmignore confusion
• With "files" you don’t have to maintain two “ignore” lists; you just list what you want.
How npm decides what to publish (simplified)
If "files" exists → only those paths (plus a few always-included ones like README*, LICENSE*).
Else if .npmignore exists → everything except patterns in .npmignore.
So "files" is the whitelist, .npmignore the blacklist. Whitelist is generally safer.
Typical CLI / TypeScript package
jsonc
{
"name": "ts-awesome-cli",
"version": "1.0.0",
"main": "dist/index.js",
"bin": { "ts-cli": "dist/cli.js" },
"files": [
"dist",
"bin", // if you split out shell scripts
"!**/*.map",// (optional) exclude source maps
"README.md",
"LICENSE"
],
"scripts": {
"prepare": "npm run build"
}
}
When you run npm publish, the resulting tarball is minimal—just dist/, bin/, the README and license.
When might you skip it?
• Massive monorepos where each package/ folder has its own .npmignore already.
• Packages that really do want to ship every checked-in file (rare).
Even then, one unnoticed test file or dotenv can bite you—so most maintainers still prefer "files".
Rule of thumb
If the list of things you do want to ship is shorter than the list you don’t want to ship (which is the usual case), add a "files" array.