12 | TypeScript Execute (tsx): The easiest way to run TypeScript in Node.js
13 |
14 | Documentation | Getting started →
15 |
Already a sponsor? Join the discussion in the Development repo!
24 | 25 | ## Sponsors 26 | 27 |
28 |
29 |
30 |
31 |
↓
13 | 14 | ```sh 15 | tsx --no-warnings --env-file=.env ./file.js 16 | ``` 17 | 18 | ::: warning Node.js version matters 19 | Under the hood, `tsx` calls `node`. This means the Node.js features supported in `tsx` depend on the Node.js version you have installed. 20 | ::: 21 | 22 | ## Flag & arguments positioning 23 | 24 | Just like with `node`, correctly positioning flags and arguments is important when using `tsx`. 25 | 26 | Place _tsx_ flags immediately after `tsx`, and place flags and arguments for your script after the script path. 27 | 28 | ```sh 29 | tsx [tsx flags] ./file.ts [flags & arguments for file.ts] 30 | ``` 31 | 32 | ## TypeScript REPL 33 | 34 | _tsx_ extends the Node.js REPL to support TypeScript, allowing interactive coding sessions directly in TypeScript. 35 | 36 | ```sh 37 | tsx 38 | ``` 39 | 40 | ::: info What is the Node.js REPL? 41 | The [Node.js REPL](https://nodejs.org/en/learn/command-line/how-to-use-the-nodejs-repl) is an interactive prompt that immediately executes input code, ideal for learning and experimenting. _tsx_ enhances this tool by adding TypeScript support. 42 | ::: 43 | 44 | ## Test runner 45 | 46 | _tsx_ enhances the Node.js built-in [test runner](https://nodejs.org/api/test.html) with TypeScript support. You can use it the same way: 47 | 48 | ```sh 49 | tsx --test 50 | ``` 51 | 52 | It will automatically recognize test files with TypeScript extensions: 53 | - `**/*.test.?[cm][jt]s` 54 | - `**/*-test.?[cm][jt]s` 55 | - `**/*_test.?[cm][jt]s` 56 | - `**/test-*.?[cm][jt]s` 57 | - `**/test.?[cm][jt]s` 58 | - `**/test/**/*.?[cm][jt]s` 59 | -------------------------------------------------------------------------------- /docs/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "docs", 3 | "private": true, 4 | "type": "module", 5 | "scripts": { 6 | "dev": "vitepress dev --open", 7 | "build": "vitepress build", 8 | "preview": "vitepress preview" 9 | }, 10 | "devDependencies": { 11 | "autoprefixer": "^10.4.19", 12 | "tailwindcss": "^3.4.3", 13 | "vitepress": "^1.1.0" 14 | } 15 | } -------------------------------------------------------------------------------- /docs/postcss.config.js: -------------------------------------------------------------------------------- 1 | export default { 2 | plugins: { 3 | tailwindcss: {}, 4 | autoprefixer: {}, 5 | }, 6 | }; 7 | -------------------------------------------------------------------------------- /docs/public/logo-dark.svg: -------------------------------------------------------------------------------- 1 | 13 | -------------------------------------------------------------------------------- /docs/public/logo-light.svg: -------------------------------------------------------------------------------- 1 | 13 | -------------------------------------------------------------------------------- /docs/public/logo-mini.svg: -------------------------------------------------------------------------------- 1 | 12 | -------------------------------------------------------------------------------- /docs/public/logos/11ty.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/arktype.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/astro.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/cheerio.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/cloudflare.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/codecov.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/date-fns.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/electron.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/figma.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/github.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/google.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/ibm.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/knip.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/mermaid.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/meta.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/microsoft.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/mozilla.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/nodejs.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/openai.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/prisma.svg: -------------------------------------------------------------------------------- 1 | 4 | -------------------------------------------------------------------------------- /docs/public/logos/salesforce.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/sentry.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/square.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/supabase.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/vercel.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/vite.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/vitest.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/vue.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/logos/webdriverio.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /docs/public/social.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/privatenumber/tsx/6af2aa9eb5f46e31e52984bd0249a7cbd0339f44/docs/public/social.png -------------------------------------------------------------------------------- /docs/shell-scripts.md: -------------------------------------------------------------------------------- 1 | # Shell scripts 2 | 3 | You can write a shell script in TypeScript by specifying _tsx_ in the [hashbang](https://bash.cyberciti.biz/guide/Shebang). The hashbang tells the shell how to run the script, making it executable. 4 | 5 | ### Project scripts 6 | 7 | If `tsx` is installed in your project, use your package manager to reference _tsx_: 8 | 9 | ::: code-group 10 | ```ts [npm] 11 | #!/usr/bin/env -S npx tsx 12 | 13 | console.log('argv:', process.argv.slice(2)) 14 | ``` 15 | 16 | ```ts [pnpm] 17 | #!/usr/bin/env -S pnpm tsx 18 | 19 | console.log('argv:', process.argv.slice(2)) 20 | ``` 21 | 22 | ```ts [yarn] 23 | #!/usr/bin/env -S yarn tsx 24 | 25 | console.log('argv:', process.argv.slice(2)) 26 | ``` 27 | ::: 28 | 29 | Make the file executable: 30 | ```sh 31 | chmod +x ./file.ts 32 | ``` 33 | 34 | Now, you can run `./file.ts` directly: 35 | ```sh 36 | ./file.ts hello world 37 | # Output: argv: [ 'hello', 'world' ] 38 | ``` 39 | 40 | 41 | ### Global scripts 42 | 43 | If `tsx` is installed globally, you can reference `tsx` directly in the hashbang: 44 | 45 | _file.ts_: 46 | ```ts 47 | #!/usr/bin/env tsx 48 | 49 | console.log('argv:', process.argv.slice(2)) 50 | ``` 51 | 52 | Don't forget to `chmod` the file to make it executable! 53 | -------------------------------------------------------------------------------- /docs/tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | export default { 3 | content: [ 4 | '**/*.md', 5 | '.vitepress/**/*.vue', 6 | ], 7 | darkMode: 'selector', 8 | plugins: [ 9 | ({ addVariant }) => addVariant('light', 'html:not(.dark) &'), 10 | ], 11 | }; 12 | -------------------------------------------------------------------------------- /docs/vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "cleanUrls": true, 3 | "headers": [ 4 | { 5 | "source": "/assets/(.*)", 6 | "headers": [ 7 | { 8 | "key": "Cache-Control", 9 | "value": "max-age=31536000, immutable" 10 | } 11 | ] 12 | } 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /docs/vscode.md: -------------------------------------------------------------------------------- 1 | # VS Code debugging 2 | 3 | If you use [VS Code](https://code.visualstudio.com), you can configure it to use _tsx_ to enhance your debugging experience. 4 | 5 | To learn more about VS Code configuration, refer to the [_Launch Configuration_](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration) documentation. 6 | 7 | ## Setup 8 | 1. Create the config file 9 | 10 | Create a launch configuration file in your project at `.vscode/launch.json`: 11 | ```json5 12 | { 13 | "version": "0.2.0", 14 | "configurations": [ 15 | /* 16 | * Each config in this array corresponds to an option 17 | * in the debug drop-down 18 | */ 19 | ], 20 | } 21 | ``` 22 | 23 | 2. Pick and choose debugging methods 24 | 25 | ::: details Method 1: Run _tsx_ from inside VSCode 26 | 27 | 1. Add the following configuration to the `configurations` array in `.vscode/launch.json`: 28 | ```json5 29 | { 30 | "name": "tsx", 31 | "type": "node", 32 | "request": "launch", 33 | 34 | // Debug current file in VSCode 35 | "program": "${file}", 36 | 37 | /* 38 | * Path to tsx binary 39 | * Assuming locally installed 40 | */ 41 | "runtimeExecutable": "tsx", 42 | 43 | /* 44 | * Open terminal when debugging starts (Optional) 45 | * Useful to see console.logs 46 | */ 47 | "console": "integratedTerminal", 48 | "internalConsoleOptions": "neverOpen", 49 | 50 | // Files to exclude from debugger (e.g. call stack) 51 | "skipFiles": [ 52 | // Node.js internal core modules 53 | "