Tsx - Use TypeScript in NPM Scripts

03/11/2024, Mon
Categories: #shell #typescript
Tags: #NodeJs

Execute ESM script on Nodejs Command Line without Compilation

When executing NPM scripts you can use node to run JavaScript files.

If you want step up the type safety of your JavaScript you would switch to using TypeScript, but then you then need something to help compile your TypeScript on demand such as ts-node.

However, now when supplying the --esm flag script ts-node, it might error out with this issue:

bad option: --esm

One of things that you can do to correct for this issue is by using another TypeScript on demand compiler tool, tsx. This alternative aims to be more compatible with ESM scripts. You can also use tsx for JavaScript files.