Skip to content

Commit 79dd6ca

Browse files
docs: website (#527)
1 parent e728b63 commit 79dd6ca

18 files changed

+2701
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ dist
2727

2828
# Link config
2929
link.config.json
30+
31+
# Vitepress
32+
docs/.vitepress/cache

docs/.vitepress/config.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { defineConfig } from 'vitepress';
2+
3+
export default defineConfig({
4+
lang: 'en-US',
5+
6+
title: 'tsx',
7+
8+
description: 'tsx (TypeScript Execute) - The easiest way to run TypeScript in Node.js',
9+
10+
lastUpdated: true,
11+
12+
cleanUrls: true,
13+
14+
ignoreDeadLinks: true,
15+
16+
metaChunk: true,
17+
18+
themeConfig: {
19+
siteTitle: 'tsx',
20+
21+
editLink: {
22+
pattern: 'https://github.com/privatenumber/tsx/edit/develop/docs/:path',
23+
text: 'Edit this page on GitHub',
24+
},
25+
26+
nav: [
27+
{ text: 'Discussions', link: 'https://github.com/privatenumber/tsx/discussions' },
28+
],
29+
30+
sidebar: [
31+
{
32+
text: 'Introduction',
33+
items: [
34+
{ text: 'What is tsx?', link: '/' },
35+
{ text: 'Getting Started', link: '/getting-started' },
36+
],
37+
},
38+
{
39+
text: 'Usage',
40+
items: [
41+
{ text: 'Basic usage', link: '/usage' },
42+
{ text: 'Watch mode', link: '/watch-mode' },
43+
{ text: 'Scripts', link: '/scripts' },
44+
]
45+
},
46+
{
47+
text: 'Integration',
48+
items: [
49+
{ text: 'Node.js', link: '/node' },
50+
{ text: 'VSCode', link: '/vscode' },
51+
]
52+
},
53+
{
54+
text: 'FAQ',
55+
link: '/faq',
56+
},
57+
],
58+
59+
socialLinks: [
60+
{
61+
icon: 'github',
62+
link: 'https://github.com/privatenumber/tsx',
63+
},
64+
],
65+
},
66+
});

docs/.vitepress/theme/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import DefaultTheme from 'vitepress/theme';
2+
import './styles.css';
3+
4+
export default DefaultTheme;

docs/.vitepress/theme/styles.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;

docs/faq.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Frequently Asked Questions
2+
3+
## How can I do __ in _tsx_?
4+
5+
It's important to remember that `tsx` is a Node.js enhancement—it's still Node underneath.
6+
7+
That said, often times you might want to ask and lookup the following instead:
8+
- _"How can I do __ in Node.js?"_
9+
- _"How can I do __ in TypeScript?"_
10+
11+
12+
## Who uses _tsx_?
13+
14+
_tsx_ currently gets <a href="https://npm.im/tsx"><img class="inline-block" src="https://badgen.net/npm/dm/tsx"></a> and is used by many projects.
15+
16+
Here are some notable ones I've found via [GitHub Search](https://github.com/search?q=path%3Apackage.json+%22%5C%22tsx%5C%22%3A+%5C%22%22&type=code):
17+
18+
### Projects
19+
- [Vite](https://github.com/vitejs/vite/blob/6cccef78a52492c24d9b28f3a1784824f34f5cc3/package.json#L83)
20+
- [Vue.js](https://github.com/vuejs/core/blob/70641fc0deb857464d24aa7ab7eaa18e2a855146/package.json#L110)
21+
- [Mermaid](https://github.com/mermaid-js/mermaid/blob/3809732e48a0822fad596d0815a6dc0e166dda94/package.json#L121)
22+
- [date-fns](https://github.com/date-fns/date-fns/blob/5c1adb5369805ff552737bf8017dbe07f559b0c6/package.json#L6123)
23+
- [Cheerio](https://github.com/cheeriojs/cheerio/blob/d0b3c2f6b57cd1f835741175d463963266be0eef/package.json#L99)
24+
25+
### Companies
26+
- Vercel: [Turbo](https://github.com/vercel/turbo/blob/adbfe4c04e3cdd31ae1916d0a5222bbc5ae2bb58/packages/turbo-repository/package.json#L20), [Serve](https://github.com/vercel/serve/blob/1ea55b1b5004f468159b54775e4fb3090fedbb2b/package.json#L61), [AI](https://github.com/vercel/ai/blob/e94fb321645bfff7ecc78bb195ccd34af1a40c74/examples/ai-core/package.json#L20)
27+
- Google: [Angular](https://github.com/angular/angular/blob/a34267b72e8994d22d47c73d45f22173304939a0/package.json#L144), [\[1\]](https://github.com/google/neuroglancer/blob/d5cc03520b24ef1c66d7fb6b3a3b49eebe87bd44/package.json#L69), [\[2\]](https://github.com/google/labs-prototypes/blob/93a0fba516d95e4fc7063b9c38d1074f69322d2d/seeds/team-experiments/package.json#L25)
28+
- GitHub: [\[1\]](https://github.com/github/docs/blob/d183c8519bb08678150e7c4b45c50fb314a2d145/package.json#L273), [\[2\]](https://github.com/github/local-action/blob/a93157e99d69c563c0368bb8fd2a3c6f5c6795ea/package.json#L53)
29+
- Square (internal projects)
30+
- Supabase: [Supabase](https://github.com/supabase/supabase/blob/34d152ce7832a1313f06012612480b9717742f73/apps/docs/package.json#L101), [\[1\]](https://github.com/supabase/stripe-sync-engine/blob/01ab4093d31fad974d85d78c52b4130779dc0eeb/package.json#L55), [\[2\]](https://github.com/supabase/storage/blob/2adeac7ddb41522df3ee30b8d4cf9071426bbe5f/package.json#L103), [\[3\]](https://github.com/supabase/orb-sync-engine/blob/e3249cca02c3a7f3b385fdd9ea1f72d5eb55fb05/apps/node-fastify/package.json#L27)
31+
- Astro: [Compiler](https://github.com/withastro/compiler/blob/17f89322a5604542735b13fdedd2664253f1e8f8/package.json#L35), [Starlight](https://github.com/withastro/starlight/blob/b2c50ea1da1aaefd1f0f08dd2f501c8dc4f04726/packages/file-icons-generator/package.json#L12), [\[1\]](https://github.com/withastro/language-tools/blob/0503392b80765c8a1292ddc9c063a1187425c187/packages/astro-check/package.json#L38)
32+
33+
34+
35+
## How does _tsx_ compare to [`ts-node`](https://github.com/TypeStrong/ts-node)?
36+
37+
`tsx` and `ts-node` are both designed for executing TypeScript in Node.js, but offer different approaches to suit user preferences.
38+
39+
- **Simple installation**
40+
41+
_tsx_ is offered as a single binary without peer dependencies, and can be used without installation (just run `npx tsx ./script.ts`). In comparison, `ts-node` requires installing TypeScript or SWC as peer dependencies.
42+
43+
- **Zero configuration**
44+
45+
_tsx_ _just works_. It doesn't require initial setup or a `tsconfig.json` file, and doesn't get in the way of running your code. This is especially important for beginners getting into TypeScript!
46+
47+
- **Sensible defaults**
48+
49+
_tsx_ employs sensible defaults based on file imports and the Node.js version, removing the need for certain `tsconfig.json` settings (that are designed for compilation rather than runtime). In comparison, _ts-node_ relies on TypeScript's defaults (e.g. [`ES3` target](https://www.typescriptlang.org/tsconfig#target)), which may be outdated.
50+
51+
- **Module adaptability**
52+
53+
_tsx_ automatically adapts between CommonJS and ESM modules, even supporting `require()` of ESM modules, facilitating a smoother transition as the Node.js ecosystem evolves.
54+
55+
- **Enhancements**
56+
57+
_tsx_ gracefully handles [new JS & TS syntax](https://esbuild.github.io/content-types/) and features based on the Node.js version. It also supports [`tsconfig.json` paths](https://www.typescriptlang.org/tsconfig#paths) out of the box.
58+
59+
- **Speed**
60+
61+
_tsx_ utilizes [esbuild](https://esbuild.github.io/faq/#:~:text=typescript%20benchmark) for rapid TypeScript compilation. In comparison, _ts-node_ uses the TypeScript compiler by default. Because _tsx_ doesn't type check, it's similar to `ts-node --esm --swc` (which uses the [SWC compiler](https://github.com/TypeStrong/ts-node#swc-1)).
62+
63+
- **Watcher**
64+
65+
As a DX bonus, _tsx_ also comes with [Watch mode](/watch-mode) to help you iterate faster!
66+
67+
For a detailed technical comparison, you can refer to this [exhaustive comparison](https://github.com/privatenumber/ts-runtime-comparison) between `tsx`, `ts-node`, and other runtimes.
68+
69+
70+
## Can/should it be used in production?
71+
72+
At the end of the day, this is something you'll have to evaluate yourself against your production needs and risk tolerance.
73+
74+
Some factors you might want to consider are:
75+
76+
- _tsx_ is Node.js enhanced, so you can expect similar levels of stability.
77+
78+
- _tsx_ uses [esbuild](https://esbuild.github.io) to transform TypeScript and ESM, and esbuild hasn't reached a stable release yet.
79+
80+
81+
Some questions you might want to ask yourself are:
82+
83+
- What are the benefits vs costs of using `tsx` in production?
84+
- Are there performance costs?
85+
86+
- Does `tsx` run my code expectedly? Are there different environments and tools being used between dev and production?
87+
88+
- Can I rely on this open source project and the maintainers?
89+
90+
## Can't find your question?
91+
92+
Try searching or asking in [GitHub Discussions](https://github.com/privatenumber/tsx/discussions)!

docs/getting-started.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Getting started
2+
3+
::: info Prerequisites
4+
You must have [Node.js](https://nodejs.org/) v18 or higher installed. tsx aims to support the [LTS versions](https://endoflife.date/nodejs) of Node.js.
5+
:::
6+
7+
## Quick try
8+
9+
If you want to try `tsx` without installing it, you can use it with [npx](https://docs.npmjs.com/cli/v8/commands/npx).
10+
11+
In your command-line, simply pass in a TypeScript file you'd like to run:
12+
13+
```sh
14+
npx tsx ./script.ts
15+
```
16+
17+
## Installation
18+
19+
### Install to a project
20+
If you want to add `tsx` as a development dependency to an npm project, `cd` into the project and run the following:
21+
22+
::: code-group
23+
```sh [npm]
24+
$ npm install -D tsx
25+
```
26+
27+
```sh [pnpm]
28+
$ pnpm install -D tsx
29+
```
30+
:::
31+
32+
#### Using `tsx`
33+
Once installed, you can invoke it with your package manager while in the project directory:
34+
35+
::: code-group
36+
```sh [npm]
37+
$ npx tsx ./file.ts
38+
```
39+
40+
```sh [pnpm]
41+
$ pnpm tsx ./file.ts
42+
```
43+
:::
44+
45+
46+
#### Using it in `scripts`
47+
48+
Common commands can be added to [`package.json#scripts`](https://docs.npmjs.com/cli/v10/using-npm/scripts)
49+
50+
You can reference `tsx` directly in the command like so (you don't need `npx`):
51+
```json5
52+
{
53+
// ...
54+
55+
"scripts": {
56+
"dev": "tsx ./file.ts"
57+
},
58+
59+
// ...
60+
}
61+
```
62+
63+
64+
### Install globally
65+
66+
If you want to use `tsx` anywhere on your computer (without [`npx`](https://docs.npmjs.com/cli/v8/commands/npx)), install it globally:
67+
68+
69+
::: code-group
70+
```sh [npm]
71+
$ npm install -g tsx
72+
```
73+
74+
```sh [pnpm]
75+
$ pnpm install -g tsx
76+
```
77+
:::
78+
79+
Then, you can call `tsx` directly:
80+
81+
```sh
82+
tsx file.ts
83+
```

docs/index.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# What is _tsx_?
2+
3+
`tsx` stands for _TypeScript Execute_, and it's a TypeScript enhanced Node.js.
4+
5+
Because `tsx` is basically an alias to `node`, you can use it the same way:
6+
7+
<div class="tsx-before-after">
8+
9+
```sh
10+
node file.js
11+
```
12+
<span class="hidden sm:block">→</span>
13+
<span class="sm:hidden">↓</span>
14+
```sh
15+
tsx file.js
16+
```
17+
</div>
18+
19+
<sub>Since it's Node.js underneath, all command-line flags are supported. Use `tsx` just like you would use `node`!</sub>
20+
21+
## Features
22+
23+
### No hassle TypeScript runner
24+
25+
- _Just run_ [TypeScript](https://www.typescriptlang.org/) code. No configuration required!
26+
27+
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!
28+
29+
<!-- There's also no configuration specifically for _tsx_. Instead, you configure Node.js (via `package.json`) and TypeScript (via `tsconfig.json`). -->
30+
31+
### CJS ↔ ESM interop
32+
33+
- Seamlessly cross-import CommonJS and ES Modules!
34+
35+
If you've encountered the following error in Node, you'll never see it again with _tsx_!
36+
37+
```
38+
Error [ERR_REQUIRE_ESM]: require() of ES Module <ESM package> from ./file.js not supported.
39+
Instead change the require of <ESM package> in ./file.js to a dynamic import() which is available in all CommonJS modules.
40+
```
41+
42+
<sub>This happens in Node.js when importing an ESM file from CommonJS, which can happen in new dependencies.</sub>
43+
44+
### Watch mode
45+
46+
- Iterate on your code faster and boost productivity!
47+
48+
As a DX bonus, _tsx_ comes with a [watcher](/watch-mode) to re-run your files whenever you save them.
49+
50+
## Limitations
51+
52+
TypeScript & ESM transformations are handled by [esbuild](https://esbuild.github.io/), so it shares some of the same limitations such as:
53+
54+
- Compatibility with `eval()` is not preserved
55+
- Only [certain `tsconfig.json` properties](https://esbuild.github.io/content-types/#tsconfig-json) are supported
56+
- [`emitDecoratorMetadata`](https://www.typescriptlang.org/tsconfig#emitDecoratorMetadata) is not supported
57+
58+
For details, refer to esbuild's [JavaScript caveats](https://esbuild.github.io/content-types/#javascript-caveats) and [TypeScript caveats](https://esbuild.github.io/content-types/#typescript-caveats) documentation.
59+
60+
61+
<style scoped>
62+
.tsx-before-after {
63+
@apply
64+
flex
65+
justify-between
66+
gap-4
67+
items-center
68+
flex-wrap
69+
sm:flex-nowrap;
70+
71+
> * {
72+
@apply
73+
w-full
74+
text-center
75+
m-0;
76+
}
77+
78+
> p {
79+
@apply sm:w-auto;
80+
}
81+
}
82+
</style>

docs/node.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
outline: deep
3+
---
4+
5+
# Node.js
6+
7+
## Hooks
8+
9+
> Previously known as _Loaders_ ([renamed in v21](https://github.com/nodejs/loaders/issues/95))
10+
11+
_tsx_ is primarily designed to be a standalone binary used in place of `node`. But sometimes, you'll want to use `node` directly. For example, when adding TypeScript & ESM support to npm-installed binaries that specify node in hashbang.
12+
13+
### Usage
14+
15+
To use `tsx` as a Node.js loader, pass it in to the [`--import`](https://nodejs.org/api/module.html#enabling) flag. This will add TypeScript & ESM support for both Module and CommonJS contexts.
16+
17+
```sh
18+
node --import tsx ./file.ts
19+
```
20+
21+
Or via the [`NODE_OPTIONS`](https://nodejs.org/api/cli.html#node_optionsoptions) environment variable:
22+
```sh
23+
NODE_OPTIONS='--import tsx' node ./file.ts
24+
```
25+
26+
::: warning
27+
When using the hook, CLI features such as [_Watch mode_](/watch-mode) will not be available.
28+
:::
29+
30+
31+
### ES Modules only
32+
33+
If you only need to add TypeScript support in a Module context, you can use the ESM loader:
34+
35+
##### Node.js v20.6.0 and above
36+
```sh
37+
node --import tsx/esm ./file.ts
38+
```
39+
40+
##### Node.js v20.5.1 and below
41+
42+
```sh
43+
node --loader tsx/esm ./file.ts
44+
```
45+
46+
### CommonJS only
47+
If you only need to add TypeScript & ESM support in a CommonJS context, you can use the CJS loader:
48+
49+
```sh
50+
node --require tsx/cjs ./file.ts
51+
```

0 commit comments

Comments
 (0)