Leanpub Header

Skip to main content

Creating NPM Package with TypeScript

Simplified TypeScript Guide to Building and Publishing Libraries

Now updated and better than ever!

The new edition of CodeSweetly’s Creating NPM Package is here. It features semantic-release automation, NPM Trusted Publishing, and advanced TypeScript modularization.

Learn how to build, organize, automate, and securely publish professional TypeScript libraries with modern, production-ready workflows.

Are you ready to deliver cleaner, smarter, and fully automated packages?

Get your copy of the updated version today. Printed copies are also available on Amazon.

Minimum price

$17.00

$17.00

You pay

$17.00

Author earns

$13.60
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.
PDF
EPUB
WEB
122
Pages
About

About

About the Book

Build, Automate, and Publish Production-Ready TypeScript Libraries the Right Way

Do you want to publish high-quality TypeScript packages to NPM without struggling with complicated setups or unreliable release scripts?

Creating NPM Package is a hands-on, step-by-step guide that walks you through every stage of building a modern TypeScript library, from setting up your project to automating production releases.

This new edition includes the latest best practices, such as using semantic-release for automated versioning and release notes, setting up secure NPM Trusted Publishing, and a new chapter on making your TypeScript codebase more modular and scalable.

If you’ve ever asked yourself questions like these:

  • How should I structure my TypeScript library?
  • How do I compile to both ESM and CommonJS correctly?
  • How do I automate versioning and changelogs without manual work?
  • How do I securely publish using modern CI workflows?
  • How do I scale my library beyond a single file?

This book gives you clear, practical answers to all of these questions.

What You’ll Learn Inside

  • Configure a clean, scalable TypeScript project
  • Compile to both ECMAScript (ESM) and CommonJS (CJS) modules
  • Properly separate source code and distribution builds
  • Define correct entry points and declaration files
  • Modularize your TypeScript codebase for maintainability
  • Test your package before and after publishing
  • Automate version management and release notes using semantic-release
  • Implement NPM Trusted Publishing for secure, tokenless releases
  • Create professional README and LICENSE files
  • Publish and validate your package in both local and production environments

This book is more than just a tutorial. It gives you a repeatable system you can use for different projects.

Why Choose This Book

  • Clear, simplified explanations without unnecessary theory
  • Real-world workflows used by professional library authors
  • Automation-first approach to eliminate manual release headaches
  • It focuses on the steps for building and publishing libraries, not just on TypeScript programming itself.

Who This Book Is For

  • TypeScript developers ready to publish reusable libraries
  • JavaScript developers transitioning to TypeScript development
  • Open-source contributors aiming to adopt professional workflows
  • Teams requiring consistent and maintainable package configurations

Whether you’re publishing your first open-source project or improving a professional release process, this book will help you deliver clean, typed, and automated TypeScript libraries without extra complexity.

Start building smarter today.

Start publishing like a pro.

You can buy a printed copy at Amazon.

Author

About the Author

CodeSweetly

Learning to be a software developer can sometimes feel overwhelming or tedious, but CodeSweetly is here to help.

Unlike other resources, CodeSweetly helps you become a confident developer by simplifying technical concepts and presenting them in a way that supports your unique learning style, so you can focus on coding sweetly.

We break down programming topics with clear tutorials, examples, cheat sheets, and illustrations made to make complex ideas easier to grasp. Our approach centers on clarity and simplicity for your success.

Whether you’re new to coding or a pro, CodeSweetly makes learning to code easier and more enjoyable.

Contents

Table of Contents

Introduction

  1. Welcome to Creating NPM Package with TypeScript!
  2. Why This Book Will Help You
  3. What You’ll Build
  4. Use the Right Tools
  5. What You Should Know First
  6. Got Questions?
  7. Let’s Begin!

Project Configuration

  1. 1. Setting Up Your System
  2. 2. Creating a Project Directory
  3. 3. Creating a package.json File
  4. 4. Initializing a Git Repository
  5. 5. Specifying the Files Git Should Ignore
  6. 6. Staging and Committing Your Project’s Changes to Git
  7. 7. Configuring a GitHub Remote Repository
  8. 8. Uploading Your Local Git Directory to the Remote Repo
  9. 9. Installing TypeScript
  10. 10. Configuring the TypeScript Compiler

Testing Package’s Code

  1. 1. Installing the Testing Tool
  2. 2. Specifying Jest as Your Project’s Test Runner Tool
  3. 3. Configuring Jest to Test TypeScript Code
  4. 4. Configuring Jest to Compile TypeScript Files
  5. 5. Setting Up Jest’s Testing Environment
  6. 6. Creating Your Code Files
  7. 7. Writing Your Test Case
  8. 8. Developing the Package’s Code
  9. 9. Running the Test

Commit Message Configuration

  1. Conventional Commits Message Syntax
  2. Enforcing the Conventional Commits Format
  3. Setting Up Husky
  4. Creating a Hook to Auto-Lint Commit Messages

Setting Up Commitlint GitHub Action

  1. 1. Create a Commitlint GitHub Action Workflow File
  2. 2. Define the Commitlint GitHub Action Workflow
  3. 3. Test the Commitlint GitHub Action Workflow

Compiling TypeScript to JavaScript

  1. How to Compile Both ECMAScript and CommonJS Modules

Distinguishing Between Source Code and Distribution Code

Specifying Items to Compile

Defining Package’s Entry Point

Specifying Package’s Declaration File

Local Testing of Unpublished Package

  1. Link-Install Your Package Globally in Your System
  2. Creating a Directory for the Demo Website
  3. Creating a package.json File
  4. Initializing a Git Repository
  5. Specifying the Files Git Should Ignore
  6. Installing TypeScript
  7. Configuring TypeScript
  8. Installing Your Package from Your System’s Global Folder to the Test App
  9. Using the Link-Installed Package in the Test App
  10. Creating the Demo Website’s Homepage
  11. Styling the Demo Website’s Elements
  12. Installing the Parcel Build Tool
  13. Specifying Parcel as Your Website’s Development Server
  14. Unlinking Your Package from the Test App
  15. Unlinking Your Package from the Global Folder

Creating README

Creating LICENSE

Publishing Package to NPM

  1. 1. Search Engine Optimization (SEO)
  2. 2. Specify the Files You Want to Publish to NPM
  3. 3. Confirm the Files NPM Will Publish
  4. 4. Confirm That Your Package Have Passing Tests
  5. 5. Compile Any Pending Code
  6. 6. Stage and Commit Any Recent Changes
  7. 7. Push Your Local Git Directory to the Remote Repo
  8. 8. Sign In or Sign Up on the NPM Website
  9. 9. Log In to NPM via the Terminal
  10. 10. Confirm If Your Package’s Name Is Available
  11. 11. Publish Your Package!

Local Testing of the Published Package

  1. 1. Install the Package
  2. 2. Import the Package
  3. 3. Run Your Local Server

Production Testing of the Published Package

  1. 1. Stage and Commit Your Changes
  2. 2. Set Up a GitHub Remote Repository for Your Demo Test App
  3. 3. Upload Your Local Git Directory to the Remote Repo
  4. 4. Sign In or Create an Account on the Vercel Website
  5. 5. Deploy Your Project to Vercel
  6. 6. Test the Package on the Live Demo Website

Updating Package’s Versions

  1. Example 1: Updating to a Patch Version
  2. Example 2: Updating to a Minor Version
  3. Example 3: Updating to a Major Version

Automating Version Management

  1. 1. Create a Release GitHub Action Workflow File
  2. 2. Define the Release GitHub Action Workflow
  3. 3. How to Overwrite semantic-release’s Default Configurations
  4. 4. Notify Developers That the Package Uses Automated Version Management

NPM Trusted Publishing Configuration

  1. Add a New Feature
  2. Update an Existing Feature

Automating GitHub Releases

  1. Add the Preset Library to Your Release Workflow
  2. Configure semantic-release to Automatically Publish the Package’s Release Notes

Modularizing TypeScript Codebase

  1. 1. Identify Independent Elements
  2. 2. Split the Elements You Want to Extract into Their Separate Modules
  3. 3. Import the Extracted Elements into the tweetButton.ts File
  4. 4. Configure Jest to Strip the Extension from Import Statements
  5. 5. Release the Latest Version of the Project

Epilogue

  1. What’s Next?
  2. One Last Favor

Get the free Community Edition

You can get the free Community Edition in PDF or EPUB just by sharing your name and email address with the author, or you can just click this link to read a shorter sample online...

 

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub