Publishing Your TypeScript Package to npm: A Step-by-Step Guide
Publishing a TypeScript package to npm is one of those things that sounds simple until you hit the first "module not found" error from a consumer. Getting...
14 Apr 2024 · 2 min read

Publishing a TypeScript package to npm is one of those things that sounds simple until you hit the first "module not found" error from a consumer. Getting the build output, type declarations, and module formats right takes a few deliberate steps.
Here's how I do it.
Step 1: Initialize the package
npm init
Fill in the name, version, and description. Nothing fancy.
Step 2: Set up Git
git init
Point the remote at GitHub or wherever you host.
Step 3: Configure package.json
This is where most people get tripped up. You need to tell npm where the built files live and what formats you support:
{
"type": "module",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": ["dist"]
}
main is for CommonJS consumers. module is for ESM. types points to your type declarations. files ensures only the dist folder gets published — not your source, not your tests.
Step 4: Set up tsup
I use tsup for building. It handles dual CJS/ESM output and declaration files with minimal config.
npm install tsup --save-dev
Create a tsup.config.ts:
import { defineConfig } from "tsup";
export default defineConfig({
entry: ["src/index.ts"],
format: ["cjs", "esm"],
dts: true,
clean: true,
});
Step 5: Publish
npm publish
The trade-off
tsup makes the build trivial. The cost: it's another dependency, and if you need fine-grained control over the output, you'll eventually outgrow it and reach for raw tsc + bundler config.
For most packages, tsup is the right call. Ship it, move on.
Ehsan Gazar helps software engineers take LLM and agent systems from demo to production. He writes about building LLM systems that hold up in production: evals, tracing, RAG, agent architecture and cost control. He teaches the Senior to Staff AI Engineer cohort on Maven, where engineers learn to design and defend these systems.
Forward Deployed AI Engineering: From Customer Problem to Production Value
A live Maven cohort for engineers: 10 live 90-minute sessions taking one AI engagement from customer problem to production, ending with eight artefacts and a working slice in your own repo.
January cohort starts Tue 12 Jan 2027, Tue & Thu 6:30pm London. Enrolling now, 20 seats, $1,800.
View the live cohortKeep reading
- oxlint vs ESLint: Why I Replaced ESLint with oxlint
- Why You Might Want to Switch to pnpm from npm
- Lefthook vs Husky: Why I Switched and Never Looked Back
- The Frontend Toolchain Is Now Written in Rust and Go. What That Means for You
- Replace Axios with a Simple Custom Fetch Wrapper (Production-Ready Guide)