Sub topic 12
Packaging and Running the Command
Assume that you know basic TypeScript, package.json, and terminal commands. The project already contains a TypeScript entry point at src/cli.ts, a compiler configuration, and a build script. The goal here is to make the compiled program run as a command named wcl.
Add a package entry point
npm uses the bin field in package.json to map a command name to an executable file. The path is relative to the package root.
{
"name": "wcl",
"version": "1.0.0",
"type": "module",
"bin": {
"wcl": "dist/cli.js"
},
"files": [
"dist"
],
"scripts": {
"build": "tsc",
"test": "tsc && node --test dist"
}
}The type field tells Node.js to treat .js files as ECMAScript modules. The bin field says that the wcl command should run dist/cli.js. The files field limits the files included when the package is packed or published.
The TypeScript compiler must produce the file named in bin. With rootDir set to src and outDir set to dist, src/cli.ts becomes dist/cli.js.
Make the entry point executable
The compiled entry point should begin with a shebang. A shebang is a special first line that tells Unix-like systems which interpreter should run a file.
#!/usr/bin/env node
import { readFile } from "node:fs/promises";
import { countText } from "./count.js";
async function main(argv: string[]): Promise<number> {
const file = argv[0];
if (!file) {
console.error("usage: wcl <file>");
return 2;
}
try {
const text = await readFile(file, "utf8");
const result = countText(text);
console.log(`${result.lines} ${result.words} ${file}`);
return 0;
} catch (error) {
console.error(
`wcl: ${error instanceof Error ? error.message : String(error)}`
);
return 1;
}
}
process.exitCode = await main(process.argv.slice(2));TypeScript preserves the shebang when it emits JavaScript. You can therefore run the compiled file directly:
npm run build
node dist/cli.js sample.txtThe shebang becomes useful when npm creates a command wrapper for the package.
Link the command during development
npm link makes a local package available as a global development command without publishing it.
Run these commands from the project directory:
npm run build
npm link
wcl sample.txtThe first command creates dist/cli.js. The second registers the current package globally. The third runs the command through the bin mapping.
When you change the TypeScript source, build again before running the command:
npm run build
wcl README.mdThe linked command points to your local package, so it uses the latest compiled files after each build.
Check the package before publishing
Use npm pack --dry-run to inspect what would be included in an archive:
npm run build
npm pack --dry-runBecause files contains only dist, the package should include the compiled program and other required package metadata, but not the TypeScript source or local test files. Keep the bin path and the emitted file name synchronized. A typo in either place makes the installed command fail.
Decide what users should run
| Situation | Command | Purpose |
|---|---|---|
| Run compiled code directly | `node dist/cli.js file.txt` | Check the emitted program |
| Test a local command | `npm link` then `wcl file.txt` | Use the package's `bin` mapping |
| Inspect a release archive | `npm pack --dry-run` | Verify package contents |
A command-line program should return zero for success and a nonzero value for failure. The entry point sets process.exitCode after main finishes, allowing pending output to flush before Node.js exits.
Exercise
After changing src/cli.ts, what commands should you run before testing the linked wcl command?
Show answer
Run npm run build, then run wcl <file> so the command uses the newly compiled JavaScript.