Build for distribution
An application running through tsx does not need a separate build unless its deployment environment requires one. For a general-purpose npm library or command-line tool, build JavaScript so consumers do not need tsx or your TypeScript configuration.
If you need a build, pkgroll is the recommended package bundler for tsx projects and builds tsx itself. The steps below create a distributable npm package. For either approach, check types separately.
Compiling an npm package
Pkgroll reads the output entry points in package.json and maps paths in dist to source files in src. For example, ./dist/index.mjs is built from src/index.ts.
Setup
In a project with a package.json, install pkgroll. Run all commands on this page from the project root:
npm install -D pkgrollpnpm add -D pkgrollyarn add -D pkgrollCreate the source entry point:
src/index.ts
export const greet = (name: string) => `Hello, ${name}!`Add these fields to package.json, keeping your other fields and scripts:
package.json (add these fields)
{
"exports": "./dist/index.mjs",
"files": ["dist"],
"scripts": {
"build": "pkgroll"
}
}exports declares the JavaScript entry point consumers will load. files includes the build directory in the published package.
Build
Run the build script from the project root:
npm run buildpnpm buildyarn buildPkgroll creates dist/index.mjs. Check that Node.js can load it without tsx:
node --input-type=module -e "
import { greet } from './dist/index.mjs'
console.log(greet('world'))
"The command prints:
Hello, world!Run this build before packaging or publishing so the declared output files exist.
Optional output formats
The basic example produces an ES module. Configure additional entry points only when your package needs them.
Dual package (CJS & ESM)
To support both CommonJS (CJS) and ECMAScript modules (ESM), replace the basic exports field with the following fields. Keep the build script and files field from the setup:
package.json (replace the output fields)
{
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.cts",
"exports": {
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
},
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
}
}
}Declaration generation requires TypeScript; follow pkgroll's installation guidance before running the build script again. The build creates dist/index.cjs, dist/index.mjs, dist/index.d.cts, and dist/index.d.mts from src/index.ts. The module field is used by some bundlers, not by Node.js; see module formats.
Command-line package
Create the CLI source:
src/cli.ts
console.log('Hello from the CLI!')Add a bin field to package.json:
package.json (add this field)
{
"bin": {
"my-cli": "./dist/cli.mjs"
}
}Run the build script again. Pkgroll creates dist/cli.mjs with a Node.js hashbang so it can run as a command after installation. Test the generated file:
node ./dist/cli.mjsThe command prints:
Hello from the CLI!Publishing TypeScript source
Distribute JavaScript for a general-purpose Node.js package because TypeScript source depends on consumers' runtime and transformation configuration; see TypeScript configuration and compiler limitations.
You can include TypeScript declaration files for type information and source maps for debugging. See pkgroll's documentation for declaration files, dependencies, build targets, and other packaging options.
Troubleshooting
- An entry point cannot be built: check that each declared
distoutput has a matching source insrc. For example,./dist/index.mjsneedssrc/index.tsin this guide. See pkgroll's entry-point mapping if your directories differ. - Node.js cannot find the generated file: run the build script from the package root and check that its output path matches
exportsorbinbefore running or packaging it. - Declaration generation fails: check that the compiler dependency required by your TypeScript version is installed, as described in pkgroll's installation guidance.
