2023-05-01
3287
#node#typescript
Jack Franklin
17457
May 1, 2023 ⋅ 11 min read

Publishing Node modules with TypeScript and ES modules

Jack Franklin Googler building @ChromeDevTools.

Recent posts:

Eleventy Adoption Guide: Overview, Examples, And Alternatives

Eleventy adoption guide: Overview, examples, and alternatives

Eleventy (11ty) is a compelling solution for developers seeking a straightforward, performance-oriented approach to static site generation.

Nelson Michael
May 7, 2024 ⋅ 8 min read
6 CSS Tools For More Efficient And Flexible CSS Handling

6 CSS tools for more efficient and flexible CSS handling

Explore some CSS tools that offer the perfect blend of efficiency and flexibility when handling CSS, such as styled-components and Emotion.

Fimber Elemuwa
May 7, 2024 ⋅ 7 min read
Leveraging React Server Components In Redwoodjs

Leveraging React Server Components in RedwoodJS

RedwoodJS announced support for server-side rendering and RSCs in its Bighorn release. Explore this feature for when it’s production-ready.

Stephan Miller
May 6, 2024 ⋅ 9 min read
Exploring The Aha Stack: Astro, Htmx, Alpine — A Complete Tutorial With A Demo Project And Comparison To Other Stacks

Exploring the AHA stack: Tutorial, demo, and comparison

The AHA stack — Astro, htmx, and Alpine — is a solid web development stack for smaller apps that emphasize frontend speed and SEO.

Oyinkansola Awosan
May 3, 2024 ⋅ 13 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