Back to Tsx

Entry-point

docs/dev-api/entry-point.md

4.21.01.1 KB
Original Source

Entry-point

Import tsx at the top of your entry-file:

js
import 'tsx'

// Now you can load TS files
await import('./file.ts')
<!-- TODO: does this work in CJS mode? -->

Note, because of the order of static import evaluation in ESM, the enhancement only works on dynamic imports after registration.

::: danger Enhancing Node.js by loading tsx from within your source code at run-time can be unexpected for collaborators who aren’t aware of it.

When possible, it's recommended to use a more visible method, such as passing it as a CLI flag. :::

Advanced usage

CommonJS mode only

Require tsx/cjs at the top of your entry-file:

js
require('tsx/cjs')

// Now you can load TS files
require('./file.ts')

Module mode only

Import tsx/esm at the top of your entry-file:

js
import 'tsx/esm'

// Now you can load TS files
await import('./file.ts')