Entry-point
Import tsx
at the top of your entry-file:
js
import 'tsx'
// Now you can load TS files
await import('./file.ts')
Note, because of the order of static import evaluatation in ESM, the enhancement only works on dynamic imports after registeration.
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')