Running TypeScript with a runner
In the previous article, we learned how to run TypeScript code using transpilation. In this article, we will learn how to run TypeScript code using a runner.
Running TypeScript code with ts-node
ts-node is a TypeScript execution environment for Node.js. It allows you to run TypeScript code directly in Node.js without the need to compile it first. Note, however, that it does not type check your code. So we recommend to type check your code first with tsc
and then run it with ts-node
before shipping it.
To use ts-node
, you need to install it first:
npm i -D ts-node
Then you can run your TypeScript code like this:
npx ts-node example.ts
Running TypeScript code with tsx
tsx is another TypeScript execution environment for Node.js. It allows you to run TypeScript code directly in Node.js without the need to compile it first. Note, however, that it does not type check your code. So we recommend to type check your code first with tsc
and then run it with tsx
before shipping it.
To use tsx
, you need to install it first:
npm i -D tsx
Then you can run your TypeScript code like this:
npx tsx example.ts
Registering tsx
via node
If you want to use tsx
via node
, you can register tsx
via --import
:
node --import=tsx example.ts