Running Typescript scripts in node using esbuild

esbuild Jul 11, 2022

Typescript is hands down my favorite way to write Javascript these days, but sometimes when I just want to run a quick JS script to do something, I'll still reach for node ./foo/script.js. I'll sometimes reach for babel-node`, or @babel/register, but that can be pretty difficult to configure sometimes, and has some perf issues.

It does work though, and I have a post on it...

Running Typescript programs with babel-node aka how does babel register work
Running Typescript programs on demand with babel-node basically uses babel register, but how does babel register work? This post will detail it.

However, I recently discovered that ESBuild has a similar mechanism to babel-node called esbuild-runner.

esbuild-runner
Super-fast on-the-fly transpilation of modern JS, TypeScript and JSX using esbuild. Latest version: 2.2.1, last published: 10 months ago. Start using esbuild-runner in your project by running `npm i esbuild-runner`. There are 13 other projects in the npm registry using esbuild-runner.

It's SUPER easy to use as well.

> npm i esbuild esbuild-runner

Then, anytime you run a .ts file, you do...

node -r esbuild-runner/register ./path/to/script.ts

And it's stupid fast...

I hope to employ esbuild-runner in some more local developer tooling so developers don't have to pre-compile or build their tools.

Tags