TypeScript Library

Simple TypeScript library with ESM and CJS outputs

TypeScript Library

Build a small TypeScript utility library with Packem that ships both ESM and CommonJS output plus TypeScript declarations. Packem reads your package.json#exports to figure out which entries to build, so there is no entry list to maintain by hand.

Overview

This example demonstrates:

  • A TypeScript library with a single entry point
  • Dual ESM + CJS output driven by package.json#exports
  • Automatic .d.ts declaration generation
  • Tree-shakable named exports

Project Setup

Directory Structure

my-library/
├── src/
│   └── index.ts
├── package.json
├── packem.config.ts
└── tsconfig.json

Source Files

src/index.ts

export interface GreetOptions {
  shout?: boolean
}

/**
 * Greet someone by name.
 */
export function greet(name: string, options: GreetOptions = {}): string {
  const message = `Hello, ${name}!`
  return options.shout ? message.toUpperCase() : message
}

/**
 * Sum a list of numbers.
 */
export function sum(values: number[]): number {
  return values.reduce((total, value) => total + value, 0)
}

Configuration

package.json

Packem infers the entry points from the exports field. The import/require/types conditions tell Packem which formats to emit.

{
  "name": "@myorg/my-library",
  "version": "1.0.0",
  "description": "A simple TypeScript library",
  "type": "module",
  "files": [
    "dist"
  ],
  "main": "./dist/index.cjs",
  "module": "./dist/index.mjs",
  "types": "./dist/index.d.ts",
  "exports": {
    ".": {
      "import": {
        "types": "./dist/index.d.mts",
        "default": "./dist/index.mjs"
      },
      "require": {
        "types": "./dist/index.d.cts",
        "default": "./dist/index.cjs"
      }
    }
  },
  "scripts": {
    "build": "packem build",
    "dev": "packem build --watch"
  },
  "devDependencies": {
    "@visulima/packem": "^2",
    "esbuild": "^0.25.0",
    "typescript": "^5.3.0"
  }
}

TypeScript Configuration

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "declaration": true,
    "outDir": "dist",
    "rootDir": "src",
    "strict": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}

Packem Configuration

packem.config.ts

import { defineConfig } from '@visulima/packem/config'
import transformer from '@visulima/packem/transformer/esbuild'

export default defineConfig({
  transformer,
})

Declarations are auto-detected: because package.json defines a types field, Packem generates .d.ts (plus .d.mts/.d.cts) without any extra option.

Building

Run the build:

npm run build

Output Structure

dist/
├── index.mjs        # ESM entry
├── index.cjs        # CJS entry
├── index.d.mts      # ESM declarations
├── index.d.cts      # CJS declarations
└── index.d.ts       # Declarations

Usage

ESM

import { greet, sum } from '@myorg/my-library'

greet('world')              // "Hello, world!"
greet('world', { shout: true }) // "HELLO, WORLD!"
sum([1, 2, 3])              // 6

CommonJS

const { greet, sum } = require('@myorg/my-library')

greet('world') // "Hello, world!"

Next Steps

Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues