Bundle TypeScript with tsup

VerifiedSafe

tsup bundles TypeScript libraries for npm publishing with zero config and esbuild speed. Outputs ESM/CJS, .d.ts files, and optimizes code.

Sby Skills Guide Bot
DevOpsIntermediate
7/28/2026
Claude CodeCursorCodex
#bundling#typescript#tsup#esbuild#npm-publishing

Recommended for

Automated summary

A configuration guide for using tsup to bundle TypeScript libraries for npm, with dual ESM/CJS output and generated declaration files.

Strengths

  • Minimal setup covering both package.json (exports, main, module, types) and tsup.config.ts
  • Covers practical cases: multiple entry points with subpath exports, separate browser/Node builds, splitting, tree-shaking and sourcemaps
  • Provides a copy-paste-ready package.json with correct import/require conditions

Limitations

  • Only covers tsup: no comparison with Rollup, unbuild or plain tsc to help pick a tool
  • esbuild limitations (const enums, emitDecoratorMetadata, etc.) are not mentioned
  • Real npm publishing (versioning, CI, provenance) is only sketched via a truncated example
When to use it

Use it when you need to package or publish a TypeScript library to npm with ESM and CJS outputs.

When not to use it

Avoid it if you are bundling an application (Vite, Webpack) or your code relies on TypeScript features esbuild does not support.

Security analysis

Safe
Quality score86/100

The skill is purely documentation for configuring tsup, a standard esbuild-based bundler. It contains no executable shell payloads, no curl|sh pipes, no file deletion, no credential or token handling, and no attempts to disable agent safety controls. The only commands shown are a conventional `npm install -D tsup typescript` and package.json/tsup.config.ts snippets, all of which are legitimate developer workflow with no exfiltration or destructive behavior.

No concerns found

How this analysis is made

Examples

Publish a utils library
I have a set of TypeScript utility functions in src/. Set up tsup so I can publish them to npm with both ESM and CJS output plus .d.ts types.
React component library bundle
Bundle my React component library with tsup — ESM and CJS, generated TypeScript declarations, tree-shaking, and a proper exports map in package.json.
Monorepo subpath exports
Configure tsup for my internal package with separate entry points for index, client and server, and wire up the subpath exports in package.json.

name: tsup description: >- Bundle TypeScript libraries with tsup — zero-config, powered by esbuild. Use when someone asks to "bundle a TypeScript library", "build npm package", "tsup", "publish TypeScript to npm", "build ESM and CJS", "bundle with esbuild", or "library bundling with zero config". Covers ESM/CJS dual output, declaration files, tree-shaking, and npm publishing. license: Apache-2.0 compatibility: "Node.js 18+. TypeScript." metadata: author: terminal-skills version: "1.0.0" category: devops tags: ["bundling", "typescript", "tsup", "esbuild", "library"]

tsup

Overview

tsup bundles TypeScript libraries for npm publishing — zero config, powered by esbuild (100x faster than tsc). Outputs ESM + CJS dual packages, generates .d.ts declaration files, and handles tree-shaking. The standard tool for building TypeScript packages in 2025+.

When to Use

  • Publishing a TypeScript library to npm
  • Need both ESM and CJS output (dual package)
  • Building internal packages in a monorepo
  • Want fast builds without configuring Rollup/Webpack
  • Generating .d.ts files alongside bundled output

Instructions

Setup

npm install -D tsup typescript

Zero Config

// package.json — Minimal setup
{
  "name": "my-lib",
  "version": "1.0.0",
  "type": "module",
  "main": "./dist/index.cjs",
  "module": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "exports": {
    ".": {
      "import": { "types": "./dist/index.d.ts", "default": "./dist/index.js" },
      "require": { "types": "./dist/index.d.cts", "default": "./dist/index.cjs" }
    }
  },
  "files": ["dist"],
  "scripts": {
    "build": "tsup",
    "dev": "tsup --watch"
  }
}
// tsup.config.ts — Build configuration
import { defineConfig } from "tsup";

export default defineConfig({
  entry: ["src/index.ts"],
  format: ["esm", "cjs"],       // Dual ESM + CJS output
  dts: true,                     // Generate .d.ts files
  splitting: true,               // Code splitting for ESM
  clean: true,                   // Clean dist/ before build
  treeshake: true,               // Remove unused code
  sourcemap: true,
  minify: false,                 // Don't minify libraries
  outDir: "dist",
});

Multiple Entry Points

// tsup.config.ts — Library with subpath exports
import { defineConfig } from "tsup";

export default defineConfig({
  entry: {
    index: "src/index.ts",
    client: "src/client.ts",
    server: "src/server.ts",
    utils: "src/utils/index.ts",
  },
  format: ["esm", "cjs"],
  dts: true,
  clean: true,
});
// package.json — Subpath exports
{
  "exports": {
    ".": { "import": "./dist/index.js", "require": "./dist/index.cjs" },
    "./client": { "import": "./dist/client.js", "require": "./dist/client.cjs" },
    "./server": { "import": "./dist/server.js", "require": "./dist/server.cjs" },
    "./utils": { "import": "./dist/utils.js", "require": "./dist/utils.cjs" }
  }
}

Environment-Specific Builds

// tsup.config.ts — Different builds for browser and Node
import { defineConfig } from "tsup";

export default defineConfig([
  {
    entry: ["src/index.ts"],
    format: ["esm"],
    platform: "browser",          // Browser-optimized
    globalName: "MyLib",
    outDir: "dist/browser",
  },
  {
    entry: ["src/index.ts"],
    format: ["esm", "cjs"],
    platform: "node",             // Node.js optimized
    target: "node18",
    outDir: "dist/node",
    dts: true,
  },
]);

Examples

Example 1: Publish a TypeScript utility library

User prompt: "I have a collection of TypeScript utility functions. Package them for npm with ESM and CJS support."

The agent will configure tsup for dual output, set up package.json exports, generate type declarations, and prepare for npm publish.

Example 2: Build a React component library

User prompt: "Bundle my React component library with TypeScript types and tree-shaking."

The agent will set up tsup with React external, multiple entry points per component, CSS handling, and declaration file generation.

Guidelines

  • format: ["esm", "cjs"] — always ship both for maximum compatibility
  • dts: true — generate TypeScript declarations (uses a separate process)
  • clean: true — prevent stale files in dist/
  • Don't minify libraries — let the consumer's bundler handle it
  • external for peer deps — don't bundle React, Vue, etc.
  • splitting for ESM — enables tree-shaking for consumers
  • exports in package.json — modern Node.js resolution, types condition first
  • files: ["dist"] — only publish the built output
  • treeshake: true — remove internal unused code from output
  • Watch mode — tsup --watch for development

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/bundle-typescript-with-tsup-ms47vpiz/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/bundle-typescript-with-tsup-ms47vpiz/download' --output '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz/SKILL.md.part' && mv '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz/SKILL.md.part' '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

Environment declared by the author: Node.js 18+. TypeScript.

Original excerpt: Setup
npm install -D tsup typescript

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz/SKILL.md' && test -s '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/tsup' && cp -R '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz' '.claude/skills/tsup'

Cursor
test -f '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz/SKILL.md' && test -s '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/tsup' && cp -R '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz' '.cursor/skills/tsup'

Codex
test -f '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz/SKILL.md' && test -s '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz/SKILL.md' && mkdir -p '.agents/skills' && test ! -e '.agents/skills/tsup' && cp -R '.skill-downloads/bundle-typescript-with-tsup-ms47vpiz' '.agents/skills/tsup'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/bundle-typescript-with-tsup-ms47vpiz'

openskills read 'bundle-typescript-with-tsup-ms47vpiz'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/bundle-typescript-with-tsup-ms47vpiz/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills