TypeScript and native ESM on Node.js

submited by
Style Pass
2021-07-07 13:30:07

In this blog post, I’ll explain everything you need to know in order to use and produce native ECMAScript modules on Node.js.

By default, VS Code does not add filename extensions when it adds imports for us. That can be changed via the following two settings:

Package exports or package entry points let us define nicer module specifiers for importing. Recall that this is the file structure of the package:

Node.js supports more exports features, for example conditional exports: We can change our mapping depending on how our package is imported – e.g. via import or via require() (example taken from the Node.js documentation):

So far, I have only tested this setup on Node.js, but I’d assume that bundlers will support these features, too. If not already, then eventually.

I’m happy to see that ESM is supported increasingly well everywhere. For example, I moved the test-driven exercises for my book “JavaScript for impatient programmers” (in plain JavaScript) to native ESM on Node.js a while ago, and that worked really well.

Leave a Comment