CLI usage
Use the tsx command to run TypeScript and JavaScript with Node.js. The examples on this page assume tsx is installed in your project and run from the project directory.
Swap node for tsx
Use npx tsx in place of node to run a script. For example, save the following file in your project directory:
file.ts
console.log(process.argv.slice(2))Run it with two script arguments:
npx tsx ./file.ts --name AdaIt prints:
[ '--name', 'Ada' ]tsx runs your code in Node.js and accepts Node.js command-line flags, subject to the installed Node.js version's capabilities.
Flag & arguments positioning
Place tsx and Node.js flags before the script path. Place arguments intended for your script after the path:
The command has this form:
npx tsx [runtime flags] ./file.ts [script arguments]Run the file.ts example above with a Node.js flag and two script arguments:
npx tsx --no-warnings ./file.ts --name AdaIt prints:
[ '--name', 'Ada' ]The --no-warnings flag configures Node.js. The script receives --name and Ada as arguments.
tsx options
| Option | Type | Default | Purpose |
|---|---|---|---|
--tsconfig <path> | String | Not set; detect from the working directory | Use a custom TypeScript configuration. |
--no-cache | Boolean | false | Disable caching of transformed code. |
--help, -h | Boolean | false | Show tsx and Node.js CLI help. |
--version, -v | Boolean | false | Show the tsx and Node.js versions. |
The script path is an optional string. With a path, tsx executes that file and forwards the script's exit status. Without a path or another execution option, tsx opens the REPL below. Script arguments are strings passed after the path and are available through process.argv.
Use tsx watch to rerun a script when its files change.
TypeScript REPL
Run npx tsx without a script path to open an interactive TypeScript prompt, also called a REPL:
npx tsxAt the prompt, enter each line and press Return:
const message: string = 'Hello, TypeScript!'
message.toUpperCase()Output from the second expression:
'HELLO, TYPESCRIPT!'Press Ctrl+D to exit. See the Node.js REPL documentation for prompt commands and other interactive features.
Test runner
Use Node.js's --test flag with tsx to run TypeScript tests. See Testing for a complete example and discovery behavior.
