Skip to content

Commit

Permalink
Update tsx example (#6750)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyBooth committed Jun 2, 2024
1 parent 98995cd commit b631f5b
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pages/en/learn/getting-started/nodejs-with-typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,26 @@ Then you can run your TypeScript code like this:
npx ts-node example.ts
```

### Running TypeScript Code with a Node.js Loader
### Running TypeScript Code with `tsx`

Since Node.js v19.0.0, you can use a [custom loader](https://nodejs.org/api/cli.html#--importmodule). Download a loader such as `ts-node` or `tsx` or `nodejs-loaders`
You can use [tsx](https://tsx.is/) to run TypeScript code directly in Node.js without the need to compile it first. But it's not typechecking your code. So we recommend to type check your code first with `tsc` and then run it with `tsx` before shipping it.

First you need to install the loader:
To use `tsx`, you need to install it first:

```bash
npm i -D ts-node
npm i -D tsx
```

Then you can run your TypeScript code like this:

```bash
node --loader=ts-node/esm example.ts
npx tsx example.ts
```

If you want to use `tsx` via `node`, you can register `tsx` via `--import`:

```bash
node --import=tsx example.ts
```

## TypeScript in the Node.js world
Expand Down

0 comments on commit b631f5b

Please sign in to comment.