You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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))
167
170
168
-
```typescript
171
+
```typescript twoslash
169
172
#!/usr/bin/env-Sts-node--files
170
173
// This shebang works on Mac and Linux with newer versions of env
171
174
// Technically, Mac allows omitting `-S`, but Linux requires it
172
175
```
173
176
174
177
To write scripts with maximum portability, [specify all options in your `tsconfig.json`](#via-tsconfigjson-recommended) and omit them from the shebang.
175
178
176
-
```typescript
179
+
```typescript twoslash
177
180
#!/usr/bin/envts-node
178
181
// This shebang works everywhere
179
182
```
@@ -302,6 +305,7 @@ All command-line flags support both `--camelCase` and `--hyphen-case`.
302
305
*`-e, --eval` Evaluate code
303
306
*`-p, --print` Print result of `--eval`
304
307
*`-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
305
309
306
310
## TSConfig
307
311
@@ -361,7 +365,7 @@ Here is a brief comparison of the two.
| Transforms `import` into `require()`| Does not transform `import`|
363
367
| 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`|
365
369
366
370
## CommonJS
367
371
@@ -415,10 +419,36 @@ You must set [`"type": "module"`](https://nodejs.org/api/packages.html#packages_
415
419
{
416
420
"compilerOptions": {
417
421
"module":"ESNext"// or ES2015, ES2020
422
+
},
423
+
"ts-node": {
424
+
// Tell ts-node CLI to install the --loader automatically, explained below
425
+
"esm":true
418
426
}
419
427
}
420
428
```
421
429
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.
@@ -490,7 +520,7 @@ the [tsconfig `"target"` option](https://www.typescriptlang.org/tsconfig#target)
490
520
491
521
For example, `node` 12 does not understand the `?.` optional chaining operator. If you use `"target": "esnext"`, then the following TypeScript syntax:
492
522
493
-
```typescript
523
+
```typescript twoslash
494
524
const bar:string|undefined=foo?.bar;
495
525
```
496
526
@@ -606,15 +636,15 @@ Example project structure:
606
636
607
637
Example module declaration file:
608
638
609
-
```typescript
639
+
```typescript twoslash
610
640
declare module'<module_name>' {
611
641
// module definitions go here
612
642
}
613
643
```
614
644
615
645
For module definitions, you can use [`paths`](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping):
616
646
617
-
```jsonc
647
+
```jsonc title="tsconfig.json"
618
648
{
619
649
"compilerOptions": {
620
650
"baseUrl":".",
@@ -627,9 +657,11 @@ For module definitions, you can use [`paths`](https://www.typescriptlang.org/doc
627
657
628
658
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:
629
659
630
-
```typescript
631
-
/// <reference types="./types/untyped_js_lib" />
632
-
importUntypedJsLibfrom"untyped_js_lib"
660
+
```typescript twoslash
661
+
/// <reference path="./types/untyped_js_lib" />
662
+
import {Greeter} from"untyped_js_lib"
663
+
constg=newGreeter();
664
+
g.sayHello();
633
665
```
634
666
635
667
**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
737
769
738
770
The following example tells ts-node to execute a webpack config as CommonJS:
739
771
740
-
```jsonc title=tsconfig.json
772
+
```jsonc title="tsconfig.json"
741
773
{
742
774
"ts-node": {
743
775
"transpileOnly":true,
@@ -783,7 +815,7 @@ Assuming you are configuring AVA via your `package.json`, add one of the followi
783
815
784
816
Use this configuration if your `package.json` does not have `"type": "module"`.
785
817
786
-
```jsonc title"package.json"
818
+
```jsonc title="package.json"
787
819
{
788
820
"ava": {
789
821
"extensions": [
@@ -800,7 +832,7 @@ Use this configuration if your `package.json` does not have `"type": "module"`.
800
832
801
833
This configuration is necessary if your `package.json` has `"type": "module"`.
0 commit comments