Skip to content

Commit e842c11

Browse files
committed
Rebuild readme for 10.7.0
1 parent 2163398 commit e842c11

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

README.md

+45-13
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,30 @@ ts-node-transpile-only script.ts
153153

154154
# Equivalent to ts-node --cwdMode
155155
ts-node-cwd script.ts
156+
157+
# Equivalent to ts-node --esm
158+
ts-node-esm script.ts
156159
```
157160

158161
## Shebang
159162

160-
```typescript
163+
```typescript twoslash
161164
#!/usr/bin/env ts-node
162165

163166
console.log("Hello, world!")
164167
```
165168

166169
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))
167170

168-
```typescript
171+
```typescript twoslash
169172
#!/usr/bin/env -S ts-node --files
170173
// This shebang works on Mac and Linux with newer versions of env
171174
// Technically, Mac allows omitting `-S`, but Linux requires it
172175
```
173176

174177
To write scripts with maximum portability, [specify all options in your `tsconfig.json`](#via-tsconfigjson-recommended) and omit them from the shebang.
175178

176-
```typescript
179+
```typescript twoslash
177180
#!/usr/bin/env ts-node
178181
// This shebang works everywhere
179182
```
@@ -302,6 +305,7 @@ All command-line flags support both `--camelCase` and `--hyphen-case`.
302305
* `-e, --eval` Evaluate code
303306
* `-p, --print` Print result of `--eval`
304307
* `-i, --interactive` Opens the REPL even if stdin does not appear to be a terminal
308+
* `--esm` Bootstrap with the ESM loader, enabling full ESM support
305309

306310
## TSConfig
307311

@@ -361,7 +365,7 @@ Here is a brief comparison of the two.
361365
| Write native `import` syntax | Write native `import` syntax |
362366
| Transforms `import` into `require()` | Does not transform `import` |
363367
| 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) |
364-
| 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` |
368+
| Use any of:<br/>`ts-node`<br/>`node -r ts-node/register`<br/>`NODE_OPTIONS="ts-node/register" node`<br/>`require('ts-node').register({/* options */})` | Use any of:<br/>`ts-node --esm`<br/>`ts-node-esm`<br/>Set `"esm": true` in `tsconfig.json`<br />`node --loader ts-node/esm`<br/>`NODE_OPTIONS="--loader ts-node/esm" node` |
365369

366370
## CommonJS
367371

@@ -415,10 +419,36 @@ You must set [`"type": "module"`](https://nodejs.org/api/packages.html#packages_
415419
{
416420
"compilerOptions": {
417421
"module": "ESNext" // or ES2015, ES2020
422+
},
423+
"ts-node": {
424+
// Tell ts-node CLI to install the --loader automatically, explained below
425+
"esm": true
418426
}
419427
}
420428
```
421429

430+
You must also ensure node is passed `--loader`. The ts-node CLI will do this automatically with our `esm` option.
431+
432+
> Note: `--esm` must spawn a child process to pass it `--loader`. This may change if node adds the ability to install loader hooks
433+
> into the current process.
434+
435+
```shell
436+
# pass the flag
437+
ts-node --esm
438+
# Use the convenience binary
439+
ts-node-esm
440+
# or add `"esm": true` to your tsconfig.json to make it automatic
441+
ts-node
442+
```
443+
444+
If you are not using our CLI, pass the loader flag to node.
445+
446+
```shell
447+
node --loader ts-node/esm ./index.ts
448+
# Or via environment variable
449+
NODE_OPTIONS="--loader ts-node/esm" node ./index.ts
450+
```
451+
422452
# Troubleshooting
423453

424454
## Understanding configuration
@@ -490,7 +520,7 @@ the [tsconfig `"target"` option](https://www.typescriptlang.org/tsconfig#target)
490520

491521
For example, `node` 12 does not understand the `?.` optional chaining operator. If you use `"target": "esnext"`, then the following TypeScript syntax:
492522

493-
```typescript
523+
```typescript twoslash
494524
const bar: string | undefined = foo?.bar;
495525
```
496526

@@ -606,15 +636,15 @@ Example project structure:
606636
607637
Example module declaration file:
608638
609-
```typescript
639+
```typescript twoslash
610640
declare module '<module_name>' {
611641
// module definitions go here
612642
}
613643
```
614644
615645
For module definitions, you can use [`paths`](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping):
616646
617-
```jsonc
647+
```jsonc title="tsconfig.json"
618648
{
619649
"compilerOptions": {
620650
"baseUrl": ".",
@@ -627,9 +657,11 @@ For module definitions, you can use [`paths`](https://www.typescriptlang.org/doc
627657
628658
An alternative approach for definitions of third-party libraries are [triple-slash directives](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html). This may be helpful if you prefer not to change your TypeScript `compilerOptions` or structure your custom type definitions when using `typeRoots`. Below is an example of the triple-slash directive as a relative path within your project:
629659
630-
```typescript
631-
/// <reference types="./types/untyped_js_lib" />
632-
import UntypedJsLib from "untyped_js_lib"
660+
```typescript twoslash
661+
/// <reference path="./types/untyped_js_lib" />
662+
import {Greeter} from "untyped_js_lib"
663+
const g = new Greeter();
664+
g.sayHello();
633665
```
634666
635667
**Tip:** If you *must* use `files`, `include`, or `exclude`, enable `--files` flags or set `TS_NODE_FILES=true`.
@@ -737,7 +769,7 @@ CommonJS or ESM. Node supports similar overriding via `.cjs` and `.mjs` file ex
737769

738770
The following example tells ts-node to execute a webpack config as CommonJS:
739771

740-
```jsonc title=tsconfig.json
772+
```jsonc title="tsconfig.json"
741773
{
742774
"ts-node": {
743775
"transpileOnly": true,
@@ -783,7 +815,7 @@ Assuming you are configuring AVA via your `package.json`, add one of the followi
783815

784816
Use this configuration if your `package.json` does not have `"type": "module"`.
785817

786-
```jsonc title"package.json"
818+
```jsonc title="package.json"
787819
{
788820
"ava": {
789821
"extensions": [
@@ -800,7 +832,7 @@ Use this configuration if your `package.json` does not have `"type": "module"`.
800832

801833
This configuration is necessary if your `package.json` has `"type": "module"`.
802834

803-
```jsonc title"package.json"
835+
```jsonc title="package.json"
804836
{
805837
"ava": {
806838
"extensions": {

0 commit comments

Comments
 (0)