Jack Franklin
May 1, 2023 ⋅ 11 min read

Publishing Node modules with TypeScript and ES modules

Jack Franklin Googler building @ChromeDevTools.

Recent posts:

Implementing Infinite Scroll In Next Js With Server Actions

Implementing infinite scroll in Next.js with Server Actions

Infinite scrolling in Next.js no longer requires external libraries — Server Actions let us fetch initial data directly on the server.

Rahul Chhodde
Apr 19, 2024 ⋅ 10 min read
Integrating Django Templates With React For Dynamic Webpages

Integrating Django templates with React for dynamic webpages

Create a dynamic demo blog site using Django and React to demonstrate Django’s server-side functionalities and React’s interactive UI.

Kayode Adeniyi
Apr 18, 2024 ⋅ 7 min read
Using Aoi Js To Build A Bot For Discord

Using aoi.js to build a bot on Discord

Explore how the aoi.js library makes it easy to create Discord bots with useful functionalities for frontend applications.

Rahul Padalkar
Apr 17, 2024 ⋅ 9 min read
Web Components Adoption Guide: Overview, Examples, And Alternatives

Web Components adoption guide: Overview, examples, and alternatives

Evaluate Web Components, a set of standards that allow you to create custom HTML tags for more reusable, manageable code.

Elijah Asaolu
Apr 16, 2024 ⋅ 11 min read
View all posts

12 Replies to "Publishing Node modules with TypeScript and ES modules"

  1. Thanks for the great article, I’ve really enjoyed it!

    Wanted to mention a possible slip: on section “Preparing to publish our module” -> “Prepublish” package.json part, inside the “scripts” object “prepublish” is used, although the article mentions using “prepublishOnly”. If I’m misunderstanding something please ignore this segment of the comment. 🙂

  2. Super simple, clear and objective tutorial, I’m not even from a webdev background and could be able to build a simple typescript based package for node and web just following this tutorial.

    Thank’s a lot !

  3. It was a very useful article for me.

    Based on this article, I was able to reduce the bundle size of my work by 20KB.
    (It was a small but very meaningful change.)

    Thank you very much.

  4. You should really read the discussion on this issue: https://github.com/microsoft/TypeScript/issues/15833

    The short version is that, depending on the consumer’s build toolchain, it’s possible to wind up importing both the ESM and CJS versions of the library *at the same time*, if you publish both like this. The latest advice from that issue is to only publish CJS to NPM for now, unless the “module working group” figures out a way to ensure loaders only load one or the other.

    1. It’s a nonsense. Most mainstream packages bundle both CJS and ESM and are perfectly fine. And the only person in that thread who suggested it’s dangerous was You.

      1. Wes Wigham, a TS team member, says

        > Attempting to ship esm “side-by-side” is just going to create runtime confusion as you have the esm version and the cjs version of your package both being included via different means

        Do you have examples of “mainstream packages” that ship both types? I would genuinely like to follow best practices and it’s always good to have a well-tested model to follow. (Angular provides both via a complex series of post-install hooks, which sounds like a terrible idea for small general-purpose libraries.)

  5. Hi..This was a great help. Although, I have a question. Say, I used some dependencies in my add.ts or subtract.ts. How can I ship the complete package along wiyh the dependencies code.

  6. Really awesome write up! I went through a ton of articles on the subject and struggled with this for 2-3 days. Nothing worked until I followed your instructions step-by-step. Thank you!

Leave a Reply