All Articles

How to Run Scripts with TypeScript and esr

Joe PreviteJoe Previte

For the past couple of years, the main way to run scripts written in TypeScript has been with ts-node. However, now we have something faster and more modern - esbuild-runner or esr for short.

It's very fast and very easy to start using today.

💡Tip

esr makes it easy to run arbitrary code or tests without needing to build your whole project. It's a great way to improve your development workflow.

How to Use

In 2 mins or less, let me show you exactly how to use it:

  1. yarn init -y - create a new project
  2. yarn add -D esbuild esbuild-runner - add dependencies
  3. echo "console.log('hello world')" > hello-world.ts - create a script
  4. yarn esr hello-world.ts - run your script

After that, you should see the following output in your terminal:

yarn run v1.22.15
$ /esr-test/node_modules/.bin/esr hello-world.ts
hello world
Done in 0.23s

Lightning fast!

And with that new knowledge, you can now migrate your project's scripts over from Bash to TypeScript. Woohoo!