Getting started
Prerequisites
Before you can start using tsx, ensure that you have Node.js installed. tsx is designed to be compatible with all maintained versions of Node.js.
Quickstart
tsx
can be executed with npx—a tool to run npm packages without installing them.
In your command-line, simply pass in a TypeScript file you'd like to run. It's that simple!
npx tsx ./script.ts
Project installation
To install tsx
as a project development dependency, run the following command in your project directory:
$ npm install -D tsx
$ pnpm add -D tsx
$ yarn add -D tsx
Using tsx
Once installed, you can invoke it with your package manager while in the project directory:
$ npx tsx ./file.ts
$ pnpm tsx ./file.ts
$ yarn tsx ./file.ts
Using it in package.json#scripts
Project commands are usually organized in the package.json#scripts
object.
In the scripts
object, you can reference tsx
directly without npx
:
// package.json
{
"scripts": {
"start": "tsx ./file.ts"
}
}
Global installation
If you want to use tsx
anywhere on your computer (without npx
), install it globally:
$ npm install -g tsx
$ pnpm add -g tsx
Yarn 2 doesn't support global installation
https://yarnpkg.com/migration/guide#use-yarn-dlx-instead-of-yarn-global
This allows you to call tsx
directly:
tsx file.ts