What is tsx?
tsx
stands for TypeScript Execute, and it's a simple command to run TypeScript in Node.js.
Because tsx
is basically an alias to node
, you can use it the same way:
node file.js
→↓
tsx file.js
Since it's Node.js underneath, all command-line flags are supported. Use tsx
as you would use node
!
Features
No hassle TypeScript runner
Just run TypeScript code. No configuration required!
The primary goal of tsx is to run TypeScript code with modern and sensible defaults. This makes tsx very user-friendly and great for beginners!
CJS ↔ ESM interop
Seamlessly cross-import CommonJS and ES Modules!
If you've encountered the following error in Node, you'll never see it again with tsx!
Error [ERR_REQUIRE_ESM]: require() of ES Module <ESM package> from ./file.js not supported. Instead change the require of <ESM package> in ./file.js to a dynamic import() which is available in all CommonJS modules.
This happens in Node.js when importing an ESM file from CommonJS, which can happen in new dependencies.
Watch mode
Iterate on your code faster and boost productivity!
As a DX bonus, tsx comes with a watcher to re-run your files whenever you save them.
Limitations
TypeScript & ESM transformations are handled by esbuild, so it shares some of the same limitations such as:
- Compatibility with
eval()
is not preserved - Only certain
tsconfig.json
properties are supportedemitDecoratorMetadata
is not supported
For details, see esbuild's JavaScript caveats and TypeScript caveats documentation.