Skip to content

Commit 3ca5df8

Browse files
committed
update the readme
1 parent 45524ae commit 3ca5df8

File tree

1 file changed

+47
-50
lines changed

1 file changed

+47
-50
lines changed

README.md

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@ You can build the readme with this command:
1717
[![Build status](https://img.shields.io/github/workflow/status/TypeStrong/ts-node/Continuous%20Integration)](https://github.com/TypeStrong/ts-node/actions?query=workflow%3A%22Continuous+Integration%22)
1818
[![Test coverage](https://codecov.io/gh/TypeStrong/ts-node/branch/main/graph/badge.svg)](https://codecov.io/gh/TypeStrong/ts-node)
1919

20-
> TypeScript execution and REPL for node.js, with source map support. **Works with `typescript@>=2.7`**.
20+
> TypeScript execution and REPL for node.js, with source map and native ESM support.
2121
2222
The latest documentation can also be found on our website: <https://typestrong.org/ts-node>
2323

24-
### *Experimental ESM support*
25-
26-
Native ESM support is currently experimental. For usage, limitations, and to provide feedback, see [#1007](https://github.com/TypeStrong/ts-node/issues/1007).
27-
2824
# Table of Contents
2925

3026
* [Overview](#overview)
@@ -66,7 +62,7 @@ Native ESM support is currently experimental. For usage, limitations, and to pro
6662
* [Skipping `node_modules`](#skipping-node_modules)
6763
* [paths and baseUrl
6864
](#paths-and-baseurl)
69-
* [Why is this not built-in to `ts-node`?](#why-is-this-not-built-in-to-ts-node)
65+
* [Why is this not built-in to ts-node?](#why-is-this-not-built-in-to-ts-node)
7066
* [Help! My Types Are Missing!](#help-my-types-are-missing)
7167
* [Third-party compilers](#third-party-compilers)
7268
* [Third-party transpilers](#third-party-transpilers)
@@ -91,7 +87,7 @@ Native ESM support is currently experimental. For usage, limitations, and to pro
9187

9288
# Overview
9389

94-
`ts-node` is a TypeScript execution engine and REPL for Node.js.
90+
ts-node is a TypeScript execution engine and REPL for Node.js.
9591

9692
It JIT transforms TypeScript into JavaScript, enabling you to directly execute TypeScript on Node.js without precompiling.
9793
This is accomplished by hooking node's module loading APIs, enabling it to be used seamlessly alongside other Node.js
@@ -170,27 +166,27 @@ Passing CLI arguments via shebang is allowed on Mac but not Linux. For example,
170166
#!/usr/bin/env ts-node --files
171167
// This shebang is not portable. It only works on Mac
172168

173-
Instead, specify all `ts-node` options in your `tsconfig.json`.
169+
Instead, specify all ts-node options in your `tsconfig.json`.
174170

175171
## Programmatic
176172

177-
You can require `ts-node` and register the loader for future requires by using `require('ts-node').register({ /* options */ })`. You can also use file shortcuts - `node -r ts-node/register` or `node -r ts-node/register/transpile-only` - depending on your preferences.
173+
You can require ts-node and register the loader for future requires by using `require('ts-node').register({ /* options */ })`. You can also use file shortcuts - `node -r ts-node/register` or `node -r ts-node/register/transpile-only` - depending on your preferences.
178174

179-
**Note:** If you need to use advanced node.js CLI arguments (e.g. `--inspect`), use them with `node -r ts-node/register` instead of the `ts-node` CLI.
175+
**Note:** If you need to use advanced node.js CLI arguments (e.g. `--inspect`), use them with `node -r ts-node/register` instead of ts-node's CLI.
180176

181177
### Developers
182178

183-
`ts-node` exports a `create()` function that can be used to initialize a TypeScript compiler that isn't registered to `require.extensions`, and it uses the same code as `register`.
179+
ts-node exports a `create()` function that can be used to initialize a TypeScript compiler that isn't registered to `require.extensions`, and it uses the same code as `register`.
184180

185181
# Configuration
186182

187-
`ts-node` supports a variety of options which can be specified via `tsconfig.json`, as CLI flags, as environment variables, or programmatically.
183+
ts-node supports a variety of options which can be specified via `tsconfig.json`, as CLI flags, as environment variables, or programmatically.
188184

189185
For a complete list, see [Options](#options).
190186

191187
## CLI flags
192188

193-
`ts-node` CLI flags must come *before* the entrypoint script. For example:
189+
ts-node CLI flags must come *before* the entrypoint script. For example:
194190

195191
```shell
196192
$ ts-node --project tsconfig-dev.json say-hello.ts Ronald
@@ -199,7 +195,7 @@ Hello, Ronald!
199195

200196
## Via tsconfig.json (recommended)
201197

202-
`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.
198+
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.
203199

204200
Use `--skip-project` to skip loading the `tsconfig.json`. Use `--project` to explicitly specify the path to a `tsconfig.json`.
205201

@@ -237,7 +233,7 @@ Our bundled [JSON schema](https://unpkg.com/browse/ts-node@latest/tsconfig.schem
237233
### @tsconfig/bases
238234

239235
[@tsconfig/bases](https://github.com/tsconfig/bases) maintains recommended configurations for several node versions.
240-
As a convenience, these are bundled with `ts-node`.
236+
As a convenience, these are bundled with ts-node.
241237

242238
```jsonc title="tsconfig.json"
243239
{
@@ -250,7 +246,7 @@ As a convenience, these are bundled with `ts-node`.
250246

251247
### Default config
252248

253-
If no `tsconfig.json` is loaded from disk, `ts-node` will use the newest recommended defaults from
249+
If no `tsconfig.json` is loaded from disk, ts-node will use the newest recommended defaults from
254250
[@tsconfig/bases](https://github.com/tsconfig/bases/) compatible with your `node` and `typescript` versions.
255251
With the latest `node` and `typescript`, this is [`@tsconfig/node16`](https://github.com/tsconfig/bases/blob/master/bases/node16.json).
256252

@@ -260,15 +256,15 @@ When in doubt, `ts-node --show-config` will log the configuration being used, an
260256

261257
## `node` flags
262258

263-
[`node` flags](https://nodejs.org/api/cli.html) must be passed directly to `node`; they cannot be passed to the `ts-node` binary nor can they be specified in `tsconfig.json`
259+
[`node` flags](https://nodejs.org/api/cli.html) must be passed directly to `node`; they cannot be passed to the ts-node binary nor can they be specified in `tsconfig.json`
264260

265261
We recommend using the [`NODE_OPTIONS`](https://nodejs.org/api/cli.html#cli_node_options_options) environment variable to pass options to `node`.
266262

267263
```shell
268264
NODE_OPTIONS='--trace-deprecation --abort-on-uncaught-exception' ts-node ./index.ts
269265
```
270266

271-
Alternatively, you can invoke `node` directly and install `ts-node` via `--require`/`-r`
267+
Alternatively, you can invoke `node` directly and install ts-node via `--require`/`-r`
272268

273269
```shell
274270
node --trace-deprecation --abort-on-uncaught-exception -r ts-node/register ./index.ts
@@ -335,7 +331,7 @@ The API includes [additional options](https://typestrong.org/ts-node/api/interfa
335331

336332
# CommonJS vs native ECMAScript modules
337333

338-
TypeScript is almost always written using modern `import` syntax, but you can choose to either transform to CommonJS or use node's native ESM support. Configuration is different for each.
334+
TypeScript is almost always written using modern `import` syntax, but it is also transformed before being executed by the underlying runtime. You can choose to either transform to CommonJS or to preserve the native `import` syntax, using node's native ESM support. Configuration is different for each.
339335

340336
Here is a brief comparison of the two.
341337

@@ -344,7 +340,7 @@ Here is a brief comparison of the two.
344340
| Write native `import` syntax | Write native `import` syntax |
345341
| Transforms `import` into `require()` | Does not transform `import` |
346342
| Node executes scripts using the classic [CommonJS loader](https://nodejs.org/dist/latest-v16.x/docs/api/modules.html) | Node executes scripts using the new [ESM loader](https://nodejs.org/dist/latest-v16.x/docs/api/esm.html) |
347-
| Use any of:<br/>`ts-node` CLI<br/>`node -r ts-node/register`<br/>`NODE_OPTIONS="ts-node/register" node`<br/>`require('ts-node').register({/* options */})` | Must use the ESM loader via:<br/>`node --loader ts-node/esm`<br/>`NODE_OPTIONS="--loader ts-node/esm" node` |
343+
| Use any of:<br/>ts-node CLI<br/>`node -r ts-node/register`<br/>`NODE_OPTIONS="ts-node/register" node`<br/>`require('ts-node').register({/* options */})` | Must use the ESM loader via:<br/>`node --loader ts-node/esm`<br/>`NODE_OPTIONS="--loader ts-node/esm" node` |
348344

349345
## CommonJS
350346

@@ -365,7 +361,7 @@ Transforming to CommonJS is typically simpler and more widely supported because
365361
}
366362
```
367363

368-
If you must keep `"module": "ESNext"` for `tsc`, webpack, or another build tool, you can set an override for `ts-node`.
364+
If you must keep `"module": "ESNext"` for `tsc`, webpack, or another build tool, you can set an override for ts-node.
369365

370366
```jsonc title="tsconfig.json"
371367
{
@@ -382,8 +378,7 @@ If you must keep `"module": "ESNext"` for `tsc`, webpack, or another build tool,
382378

383379
## Native ECMAScript modules
384380

385-
[Node's ESM loader hooks](https://nodejs.org/api/esm.html#esm_experimental_loaders) are [**experimental**](https://nodejs.org/api/documentation.html#documentation_stability_index) and subject to change. `ts-node`'s ESM support is also experimental. They may have
386-
breaking changes in minor and patch releases and are not recommended for production.
381+
[Node's ESM loader hooks](https://nodejs.org/api/esm.html#esm_experimental_loaders) are [**experimental**](https://nodejs.org/api/documentation.html#documentation_stability_index) and subject to change. ts-node's ESM support is as stable as possible, but it relies on APIs which node can *and will* break in new versions of node. Thus it is not recommended for production.
387382

388383
For complete usage, limitations, and to provide feedback, see [#1007](https://github.com/TypeStrong/ts-node/issues/1007).
389384

@@ -407,11 +402,11 @@ You must set [`"type": "module"`](https://nodejs.org/api/packages.html#packages_
407402

408403
## Understanding configuration
409404

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

414-
`ts-node` also respects your locally-installed `typescript` version, but global installations fallback to the globally-installed
409+
ts-node also respects your locally-installed `typescript` version, but global installations fallback to the globally-installed
415410
`typescript`. If you are unsure which versions are used, `ts-node -vv` will log them.
416411

417412
```shell
@@ -457,15 +452,15 @@ $ ts-node --show-config
457452

458453
## Understanding Errors
459454

460-
It is important to differentiate between errors from `ts-node`, errors from the TypeScript compiler, and errors from `node`. It is also important to understand when errors are caused by a type error in your code, a bug in your code, or a flaw in your configuration.
455+
It is important to differentiate between errors from ts-node, errors from the TypeScript compiler, and errors from `node`. It is also important to understand when errors are caused by a type error in your code, a bug in your code, or a flaw in your configuration.
461456

462457
### `TSError`
463458

464459
Type errors from the compiler are thrown as a `TSError`. These are the same as errors you get from `tsc`.
465460

466461
### `SyntaxError`
467462

468-
Any error that is not a `TSError` is from node.js (e.g. `SyntaxError`), and cannot be fixed by TypeScript or `ts-node`. These are bugs in your code or configuration.
463+
Any error that is not a `TSError` is from node.js (e.g. `SyntaxError`), and cannot be fixed by TypeScript or ts-node. These are bugs in your code or configuration.
469464

470465
#### Unsupported JavaScript syntax
471466

@@ -488,11 +483,11 @@ When you try to run this code, node 12 will throw a `SyntaxError`. To fix this,
488483
489484
# Make it fast
490485
491-
These tricks will make `ts-node` faster.
486+
These tricks will make ts-node faster.
492487
493488
## Skip typechecking
494489
495-
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.
490+
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.
496491
497492
* Enable [`transpileOnly`](#options) to skip typechecking
498493
* Use our [`swc` integration](#bundled-swc-integration)
@@ -510,7 +505,7 @@ It is often better to use `tsc --noEmit` to typecheck once before your tests run
510505
511506
## How It Works
512507
513-
`ts-node` works by registering hooks for `.ts`, `.tsx`, `.js`, and/or `.jsx` extensions.
508+
ts-node works by registering hooks for `.ts`, `.tsx`, `.js`, and/or `.jsx` extensions.
514509
515510
Vanilla `node` loads `.js` by reading code from disk and executing it. Our hook runs in the middle, transforming code from TypeScript to JavaScript and passing the result to `node` for execution. This transformation will respect your `tsconfig.json` as if you had compiled via `tsc`.
516511
@@ -520,7 +515,7 @@ Vanilla `node` loads `.js` by reading code from disk and executing it. Our hook
520515
521516
> **Warning:** if a file is ignored or its file extension is not registered, node will either fail to resolve the file or will attempt to execute it as JavaScript without any transformation. This may cause syntax errors or other failures, because node does not understand TypeScript type syntax nor bleeding-edge ECMAScript features.
522517
523-
> **Warning:** When `ts-node` is used with `allowJs`, all non-ignored JavaScript files are transformed using the TypeScript compiler.
518+
> **Warning:** When ts-node is used with `allowJs`, all non-ignored JavaScript files are transformed using the TypeScript compiler.
524519
525520
### Skipping `node_modules`
526521
@@ -532,7 +527,7 @@ By default, **TypeScript Node** avoids compiling files in `/node_modules/` for t
532527
533528
## paths and baseUrl&#xA;
534529
535-
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`.
530+
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`.
536531
537532
```jsonc title="tsconfig.json"
538533
{
@@ -543,7 +538,7 @@ You can use `ts-node` together with [tsconfig-paths](https://www.npmjs.com/packa
543538
}
544539
```
545540
546-
### Why is this not built-in to `ts-node`?
541+
### Why is this not built-in to ts-node?
547542
548543
The official TypeScript Handbook explains the intended purpose for `"paths"` in ["Additional module resolution flags"](https://www.typescriptlang.org/docs/handbook/module-resolution.html#additional-module-resolution-flags).
549544
@@ -552,11 +547,11 @@ The official TypeScript Handbook explains the intended purpose for `"paths"` in
552547
> It is important to note that the compiler will not perform any of these transformations; it just uses these pieces of information to guide the process of resolving a module import to its definition file.
553548
554549
This means `"paths"` are intended to describe mappings that the build tool or runtime *already* performs, not to tell the build tool or
555-
runtime how to resolve modules. In other words, they intend us to write our imports in a way `node` already understands. For this reason, `ts-node` does not modify `node`'s module resolution behavior to implement `"paths"` mappings.
550+
runtime how to resolve modules. In other words, they intend us to write our imports in a way `node` already understands. For this reason, ts-node does not modify `node`'s module resolution behavior to implement `"paths"` mappings.
556551
557552
## Help! My Types Are Missing!
558553
559-
**TypeScript Node** does *not* use `files`, `include` or `exclude`, by default. This is because a large majority projects do not use all of the files in a project directory (e.g. `Gulpfile.ts`, runtime vs tests) and parsing every file for types slows startup time. Instead, `ts-node` starts with the script file (e.g. `ts-node index.ts`) and TypeScript resolves dependencies based on imports and references.
554+
ts-node does *not* use `files`, `include` or `exclude`, by default. This is because a large majority projects do not use all of the files in a project directory (e.g. `Gulpfile.ts`, runtime vs tests) and parsing every file for types slows startup time. Instead, ts-node starts with the script file (e.g. `ts-node index.ts`) and TypeScript resolves dependencies based on imports and references.
560555
561556
For global definitions, you can use the `typeRoots` compiler option. This requires that your type definitions be structured as type packages (not loose TypeScript definition files). More details on how this works can be found in the [TypeScript Handbook](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html#types-typeroots-and-types).
562557
@@ -637,7 +632,7 @@ For example, to use `ttypescript` and `ts-transformer-keys`, add this to your `t
637632
638633
In transpile-only mode, we skip typechecking to speed up execution time. You can go a step further and use a
639634
third-party transpiler to transform TypeScript into JavaScript even faster. You will still benefit from
640-
`ts-node`'s automatic `tsconfig.json` discovery, sourcemap support, and global `ts-node` CLI. Integrations
635+
ts-node's automatic `tsconfig.json` discovery, sourcemap support, and global ts-node CLI. Integrations
641636
can automatically derive an appropriate configuration from your existing `tsconfig.json` which simplifies project
642637
boilerplate.
643638
@@ -681,7 +676,7 @@ Integrations are `require()`d, so they can be published to npm. The module must
681676
682677
## Module type overrides
683678
684-
When deciding between CommonJS and native ECMAScript modules, `ts-node` defaults to matching vanilla `node` and `tsc`
679+
When deciding between CommonJS and native ECMAScript modules, ts-node defaults to matching vanilla `node` and `tsc`
685680
behavior. This means TypeScript files are transformed according to your `tsconfig.json` `"module"` option and executed
686681
according to node's rules for the `package.json` `"type"` field.
687682
@@ -694,7 +689,7 @@ In these situations, our `moduleTypes` option lets you override certain files, f
694689
CommonJS or ESM. Node supports similar overriding via `.cjs` and `.mjs` file extensions, but `.ts` files cannot use them.
695690
`moduleTypes` achieves the same effect, and *also* overrides your `tsconfig.json` `"module"` config appropriately.
696691
697-
The following example tells `ts-node` to execute a webpack config as CommonJS:
692+
The following example tells ts-node to execute a webpack config as CommonJS:
698693
699694
```jsonc title=tsconfig.json
700695
{
@@ -836,20 +831,22 @@ ts-node node_modules/tape/bin/tape [...args]
836831

837832
## Visual Studio Code
838833

839-
Create a new node.js configuration, add `-r ts-node/register` to node args and move the `program` to the `args` list (so VS Code doesn't look for `outFiles`).
834+
Create a new Node.js debug configuration, add `-r ts-node/register` to node args and move the `program` to the `args` list (so VS Code doesn't look for `outFiles`).
840835

841-
```jsonc
836+
```jsonc title=".vscode/launch.json"
842837
{
843-
"type": "node",
844-
"request": "launch",
845-
"name": "Launch Program",
846-
"runtimeArgs": [
847-
"-r",
848-
"ts-node/register"
849-
],
850-
"args": [
851-
"${workspaceFolder}/index.ts"
852-
]
838+
"configurations": [{
839+
"type": "node",
840+
"request": "launch",
841+
"name": "Launch Program",
842+
"runtimeArgs": [
843+
"-r",
844+
"ts-node/register"
845+
],
846+
"args": [
847+
"${workspaceFolder}/src/index.ts"
848+
]
849+
}],
853850
}
854851
```
855852

0 commit comments

Comments
 (0)