Skip to content

Commit b7f7f04

Browse files
committed
rebuild readme for 10.5.0
1 parent fba1cc1 commit b7f7f04

File tree

1 file changed

+83
-37
lines changed

1 file changed

+83
-37
lines changed

README.md

+83-37
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ You can build the readme with this command:
1010
cd website && yarn build-readme
1111
-->
1212

13-
# ![TypeScript Node](logo.svg?sanitize=true)
13+
# [![TypeScript Node](logo.svg?sanitize=true)](https://typestrong.org/ts-node)
1414

1515
[![NPM version](https://img.shields.io/npm/v/ts-node.svg?style=flat)](https://npmjs.org/package/ts-node)
1616
[![NPM downloads](https://img.shields.io/npm/dm/ts-node.svg?style=flat)](https://npmjs.org/package/ts-node)
@@ -60,13 +60,15 @@ The latest documentation can also be found on our website: <https://typestrong.o
6060
* [Advanced](#advanced-1)
6161
* [How It Works](#how-it-works)
6262
* [Skipping `node_modules`](#skipping-node_modules)
63+
* [Skipping pre-compiled TypeScript](#skipping-pre-compiled-typescript)
6364
* [paths and baseUrl
6465
](#paths-and-baseurl)
6566
* [Why is this not built-in to ts-node?](#why-is-this-not-built-in-to-ts-node)
6667
* [Help! My Types Are Missing!](#help-my-types-are-missing)
6768
* [Third-party compilers](#third-party-compilers)
68-
* [Third-party transpilers](#third-party-transpilers)
69-
* [Bundled `swc` integration](#bundled-swc-integration)
69+
* [Transpilers](#transpilers)
70+
* [swc](#swc)
71+
* [Third-party transpilers](#third-party-transpilers)
7072
* [Writing your own integration](#writing-your-own-integration)
7173
* [Module type overrides](#module-type-overrides)
7274
* [Caveats](#caveats)
@@ -146,10 +148,10 @@ ts-node -p -e '"Hello, world!"'
146148
# Pipe scripts to execute with TypeScript.
147149
echo 'console.log("Hello, world!")' | ts-node
148150

149-
# Equivalent to ts-node --transpile-only
151+
# Equivalent to ts-node --transpileOnly
150152
ts-node-transpile-only script.ts
151153

152-
# Equivalent to ts-node --cwd-mode
154+
# Equivalent to ts-node --cwdMode
153155
ts-node-cwd script.ts
154156
```
155157

@@ -161,12 +163,27 @@ ts-node-cwd script.ts
161163
console.log("Hello, world!")
162164
```
163165

164-
Passing CLI arguments via shebang is allowed on Mac but not Linux. For example, the following will fail on Linux:
166+
Passing options via shebang requires the [`env -S` flag](https://manpages.debian.org/bullseye/coreutils/env.1.en.html#S), which is available on recent versions of `env`. ([compatibility](https://github.com/TypeStrong/ts-node/pull/1448#issuecomment-913895766))
165167

166-
#!/usr/bin/env ts-node --files
167-
// This shebang is not portable. It only works on Mac
168+
```typescript
169+
#!/usr/bin/env -S ts-node --files
170+
// This shebang works on Mac and Linux with newer versions of env
171+
// Technically, Mac allows omitting `-S`, but Linux requires it
172+
```
173+
174+
To write scripts with maximum portability, [specify all options in your `tsconfig.json`](#via-tsconfigjson-recommended) and omit them from the shebang.
175+
176+
```typescript
177+
#!/usr/bin/env ts-node
178+
// This shebang works everywhere
179+
```
168180

169-
Instead, specify all ts-node options in your `tsconfig.json`.
181+
To test your version of `env` for compatibility:
182+
183+
```shell
184+
# Note that these unusual quotes are necessary
185+
/usr/bin/env --debug '-S echo foo bar'
186+
```
170187

171188
## Programmatic
172189

@@ -197,9 +214,9 @@ Hello, Ronald!
197214

198215
ts-node automatically finds and loads `tsconfig.json`. Most ts-node options can be specified in a `"ts-node"` object using their programmatic, camelCase names. We recommend this because it works even when you cannot pass CLI flags, such as `node --require ts-node/register` and when using shebangs.
199216

200-
Use `--skip-project` to skip loading the `tsconfig.json`. Use `--project` to explicitly specify the path to a `tsconfig.json`.
217+
Use `--skipProject` to skip loading the `tsconfig.json`. Use `--project` to explicitly specify the path to a `tsconfig.json`.
201218

202-
When searching, it is resolved using [the same search behavior as `tsc`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html). By default, this search is performed relative to the entrypoint script. In `--cwd-mode` or if no entrypoint is specified -- for example when using the REPL -- the search is performed relative to `--cwd` / `process.cwd()`.
219+
When searching, it is resolved using [the same search behavior as `tsc`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html). By default, this search is performed relative to the entrypoint script. In `--cwdMode` or if no entrypoint is specified -- for example when using the REPL -- the search is performed relative to `--cwd` / `process.cwd()`.
203220

204221
You can use this sample configuration as a starting point:
205222

@@ -252,7 +269,7 @@ With the latest `node` and `typescript`, this is [`@tsconfig/node16`](https://gi
252269

253270
Older versions of `typescript` are incompatible with `@tsconfig/node16`. In those cases we will use an older default configuration.
254271

255-
When in doubt, `ts-node --show-config` will log the configuration being used, and `ts-node -vv` will log `node` and `typescript` versions.
272+
When in doubt, `ts-node --showConfig` will log the configuration being used, and `ts-node -vv` will log `node` and `typescript` versions.
256273

257274
## `node` flags
258275

@@ -274,6 +291,8 @@ node --trace-deprecation --abort-on-uncaught-exception -r ts-node/register ./ind
274291

275292
`ts-node` supports `--print` (`-p`), `--eval` (`-e`), `--require` (`-r`) and `--interactive` (`-i`) similar to the [node.js CLI options](https://nodejs.org/api/cli.html).
276293

294+
All command-line flags support both `--camelCase` and `--hyphen-case`.
295+
277296
*Environment variables, where available, are in `ALL_CAPS`*
278297

279298
## Shell
@@ -287,30 +306,31 @@ node --trace-deprecation --abort-on-uncaught-exception -r ts-node/register ./ind
287306
## TSConfig
288307

289308
* `-P, --project [path]` Path to TypeScript JSON project file <br/>*Environment:* `TS_NODE_PROJECT`
290-
* `--skip-project` Skip project config resolution and loading <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_SKIP_PROJECT`
291-
* `-c, --cwd-mode` Resolve config relative to the current directory instead of the directory of the entrypoint script
292-
* `-O, --compiler-options [opts]` JSON object to merge with compiler options <br/>*Environment:* `TS_NODE_COMPILER_OPTIONS`
293-
* `--show-config` Print resolved `tsconfig.json`, including `ts-node` options, and exit
309+
* `--skipProject` Skip project config resolution and loading <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_SKIP_PROJECT`
310+
* `-c, --cwdMode` Resolve config relative to the current directory instead of the directory of the entrypoint script
311+
* `-O, --compilerOptions [opts]` JSON object to merge with compiler options <br/>*Environment:* `TS_NODE_COMPILER_OPTIONS`
312+
* `--showConfig` Print resolved `tsconfig.json`, including `ts-node` options, and exit
294313

295314
## Typechecking
296315

297-
* `-T, --transpile-only` Use TypeScript's faster `transpileModule` <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_TRANSPILE_ONLY`
298-
* `--type-check` Opposite of `--transpile-only` <br/>*Default:* `true`<br/>*Environment:* `TS_NODE_TYPE_CHECK`
299-
* `-H, --compiler-host` Use TypeScript's compiler host API <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_COMPILER_HOST`
316+
* `-T, --transpileOnly` Use TypeScript's faster `transpileModule` <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_TRANSPILE_ONLY`
317+
* `--typeCheck` Opposite of `--transpileOnly` <br/>*Default:* `true`<br/>*Environment:* `TS_NODE_TYPE_CHECK`
318+
* `-H, --compilerHost` Use TypeScript's compiler host API <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_COMPILER_HOST`
300319
* `--files` Load `files`, `include` and `exclude` from `tsconfig.json` on startup <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_FILES`
301-
* `-D, --ignore-diagnostics [code]` Ignore TypeScript warnings by diagnostic code <br/>*Environment:* `TS_NODE_IGNORE_DIAGNOSTICS`
320+
* `-D, --ignoreDiagnostics [code]` Ignore TypeScript warnings by diagnostic code <br/>*Environment:* `TS_NODE_IGNORE_DIAGNOSTICS`
302321

303322
## Transpilation
304323

305324
* `-I, --ignore [pattern]` Override the path patterns to skip compilation <br/>*Default:* `/node_modules/` <br/>*Environment:* `TS_NODE_IGNORE`
306-
* `--skip-ignore` Skip ignore checks <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_SKIP_IGNORE`
325+
* `--skipIgnore` Skip ignore checks <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_SKIP_IGNORE`
307326
* `-C, --compiler [name]` Specify a custom TypeScript compiler <br/>*Default:* `typescript` <br/>*Environment:* `TS_NODE_COMPILER`
327+
* `--swc` Transpile with [swc](#swc). Implies `--transpileOnly` <br/>*Default:* `false`
308328
* `--transpiler [name]` Specify a third-party, non-typechecking transpiler
309-
* `--prefer-ts-exts` Re-order file extensions so that TypeScript imports are preferred <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_PREFER_TS_EXTS`
329+
* `--preferTsExts` Re-order file extensions so that TypeScript imports are preferred <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_PREFER_TS_EXTS`
310330

311331
## Diagnostics
312332

313-
* `--log-error` Logs TypeScript errors to stderr instead of throwing exceptions <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_LOG_ERROR`
333+
* `--logError` Logs TypeScript errors to stderr instead of throwing exceptions <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_LOG_ERROR`
314334
* `--pretty` Use pretty diagnostic formatter <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_PRETTY`
315335
* `TS_NODE_DEBUG` Enable debug logging<br/>
316336

@@ -323,7 +343,8 @@ node --trace-deprecation --abort-on-uncaught-exception -r ts-node/register ./ind
323343
* `--scopeDir` Directory within which compiler is limited when `scope` is enabled. <br/>*Default:* First of: `tsconfig.json` "rootDir" if specified, directory containing `tsconfig.json`, or cwd if no `tsconfig.json` is loaded.<br/>*Environment:* `TS_NODE_SCOPE_DIR`
324344
* `moduleType` Override the module type of certain files, ignoring the `package.json` `"type"` field. See [Module type overrides](#module-type-overrides) for details.<br/>*Default:* obeys `package.json` `"type"` and `tsconfig.json` `"module"` <br/>*Can only be specified via `tsconfig.json` or API.*
325345
* `TS_NODE_HISTORY` Path to history file for REPL <br/>*Default:* `~/.ts_node_repl_history`<br/>
326-
* `--no-experimental-repl-await` Disable top-level await in REPL. Equivalent to node's [`--no-experimental-repl-await`](https://nodejs.org/api/cli.html#cli_no_experimental_repl_await)<br/>*Default:* Enabled if TypeScript version is 3.8 or higher and target is ES2018 or higher.<br/>*Environment:* `TS_NODE_EXPERIMENTAL_REPL_AWAIT` set `false` to disable
346+
* `--noExperimentalReplAwait` Disable top-level await in REPL. Equivalent to node's [`--no-experimental-repl-await`](https://nodejs.org/api/cli.html#cli_no_experimental_repl_await)<br/>*Default:* Enabled if TypeScript version is 3.8 or higher and target is ES2018 or higher.<br/>*Environment:* `TS_NODE_EXPERIMENTAL_REPL_AWAIT` set `false` to disable
347+
* `experimentalResolverFeatures` Enable experimental features that re-map imports and require calls to support: `baseUrl`, `paths`, `rootDirs`, `.js` to `.ts` file extension mappings, `outDir` to `rootDir` mappings for composite projects and monorepos. For details, see [#1514](https://github.com/TypeStrong/ts-node/issues/1514)<br/>*Default:* `false`<br/>*Can only be specified via `tsconfig.json` or API.*
327348

328349
## API
329350

@@ -403,7 +424,7 @@ You must set [`"type": "module"`](https://nodejs.org/api/packages.html#packages_
403424
## Understanding configuration
404425

405426
ts-node uses sensible default configurations to reduce boilerplate while still respecting `tsconfig.json` if you
406-
have one. If you are unsure which configuration is used, you can log it with `ts-node --show-config`. This is similar to
427+
have one. If you are unsure which configuration is used, you can log it with `ts-node --showConfig`. This is similar to
407428
`tsc --showConfig` but includes `"ts-node"` options as well.
408429

409430
ts-node also respects your locally-installed `typescript` version, but global installations fallback to the globally-installed
@@ -415,7 +436,7 @@ ts-node v10.0.0
415436
node v16.1.0
416437
compiler v4.2.2
417438

418-
$ ts-node --show-config
439+
$ ts-node --showConfig
419440
{
420441
"compilerOptions": {
421442
"target": "es6",
@@ -490,7 +511,7 @@ These tricks will make ts-node faster.
490511
It is often better to use `tsc --noEmit` to typecheck once before your tests run or as a lint step. In these cases, ts-node can skip typechecking.
491512
492513
* Enable [`transpileOnly`](#options) to skip typechecking
493-
* Use our [`swc` integration](#bundled-swc-integration)
514+
* Use our [`swc` integration](#swc)
494515
* This is by far the fastest option
495516
496517
## With typechecking
@@ -519,12 +540,20 @@ Vanilla `node` loads `.js` by reading code from disk and executing it. Our hook
519540
520541
### Skipping `node_modules`
521542
522-
By default, **TypeScript Node** avoids compiling files in `/node_modules/` for three reasons:
543+
By default, ts-node avoids compiling files in `/node_modules/` for three reasons:
523544
524545
1. Modules should always be published in a format node.js can consume
525546
2. Transpiling the entire dependency tree will make your project slower
526547
3. Differing behaviours between TypeScript and node.js (e.g. ES2015 modules) can result in a project that works until you decide to support a feature natively from node.js
527548
549+
If you need to import uncompiled TypeScript in `node_modules`, use [`--skipIgnore`](#transpilation) or [`TS_NODE_SKIP_IGNORE`](#transpilation) to bypass this restriction.
550+
551+
### Skipping pre-compiled TypeScript
552+
553+
If a compiled JavaScript file with the same name as a TypeScript file already exists, the TypeScript file will be ignored. ts-node will import the pre-compiled JavaScript.
554+
555+
To force ts-node to import the TypeScript source, not the precompiled JavaScript, use [`--preferTsExts`](#transpilation).
556+
528557
## paths and baseUrl&#xA;
529558
530559
You can use ts-node together with [tsconfig-paths](https://www.npmjs.com/package/tsconfig-paths) to load modules according to the `paths` section in `tsconfig.json`.
@@ -628,7 +657,7 @@ For example, to use `ttypescript` and `ts-transformer-keys`, add this to your `t
628657
}
629658
```
630659
631-
## Third-party transpilers
660+
## Transpilers
632661
633662
In transpile-only mode, we skip typechecking to speed up execution time. You can go a step further and use a
634663
third-party transpiler to transform TypeScript into JavaScript even faster. You will still benefit from
@@ -641,12 +670,11 @@ boilerplate.
641670
> For our purposes, a compiler implements TypeScript's API and can perform typechecking.
642671
> A third-party transpiler does not. Both transform TypeScript into JavaScript.
643672
644-
### Bundled `swc` integration
673+
### swc
645674
646-
We have bundled an experimental `swc` integration.
675+
swc support is built-in via the `--swc` flag or `"swc": true` tsconfig option.
647676
648-
[`swc`](https://swc.rs) is a TypeScript-compatible transpiler implemented in Rust. This makes it an order of magnitude faster
649-
than `transpileModule`.
677+
[`swc`](https://swc.rs) is a TypeScript-compatible transpiler implemented in Rust. This makes it an order of magnitude faster than vanilla `transpileOnly`.
650678
651679
To use it, first install `@swc/core` or `@swc/wasm`. If using `importHelpers`, also install `@swc/helpers`. If `target` is less than "es2015" and using either `async`/`await` or generator functions, also install `regenerator-runtime`.
652680
@@ -659,20 +687,38 @@ Then add the following to your `tsconfig.json`.
659687
```jsonc title="tsconfig.json"
660688
{
661689
"ts-node": {
662-
"transpileOnly": true,
663-
"transpiler": "ts-node/transpilers/swc-experimental"
690+
"swc": true
664691
}
665692
}
666693
```
667694

668695
> `swc` uses `@swc/helpers` instead of `tslib`. If you have enabled `importHelpers`, you must also install `@swc/helpers`.
669696
697+
### Third-party transpilers
698+
699+
The `transpiler` option allows using third-party transpiler integrations with ts-node. `transpiler` must be given the
700+
name of a module which can be `require()`d. The built-in `swc` integration is exposed as `ts-node/transpilers/swc`.
701+
702+
For example, to use a hypothetical "speedy-ts-compiler", first install it into your project: `npm install speedy-ts-compiler`
703+
704+
Then add the following to your tsconfig:
705+
706+
```jsonc title="tsconfig.json"
707+
{
708+
"ts-node": {
709+
"transpileOnly": true,
710+
"transpiler": "speedy-ts-compiler"
711+
}
712+
}
713+
```
714+
670715
### Writing your own integration
671716

672717
To write your own transpiler integration, check our [API docs](https://typestrong.org/ts-node/api/interfaces/TranspilerModule.html).
673718

674-
Integrations are `require()`d, so they can be published to npm. The module must export a `create` function matching the
675-
[`TranspilerModule`](https://typestrong.org/ts-node/api/interfaces/TranspilerModule.html) interface.
719+
Integrations are `require()`d by ts-node, so they can be published to npm for convenience. The module must export a `create` function described by our
720+
[`TranspilerModule`](https://typestrong.org/ts-node/api/interfaces/TranspilerModule.html) interface. `create` is invoked by ts-node
721+
at startup to create the transpiler. The transpiler is used repeatedly to transform TypeScript into JavaScript.
676722

677723
## Module type overrides
678724

0 commit comments

Comments
 (0)