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 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))
165
167
166
-
#!/usr/bin/env ts-node --files
167
-
// This shebang is not portable. It only works on Mac
168
+
```typescript
169
+
#!/usr/bin/env-Sts-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/envts-node
178
+
// This shebang works everywhere
179
+
```
168
180
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
+
```
170
187
171
188
## Programmatic
172
189
@@ -197,9 +214,9 @@ Hello, Ronald!
197
214
198
215
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.
199
216
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`.
201
218
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()`.
203
220
204
221
You can use this sample configuration as a starting point:
205
222
@@ -252,7 +269,7 @@ With the latest `node` and `typescript`, this is [`@tsconfig/node16`](https://gi
252
269
253
270
Older versions of `typescript` are incompatible with `@tsconfig/node16`. In those cases we will use an older default configuration.
254
271
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.
`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).
276
293
294
+
All command-line flags support both `--camelCase` and `--hyphen-case`.
295
+
277
296
*Environment variables, where available, are in `ALL_CAPS`*
*`-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`
308
328
*`--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`
310
330
311
331
## Diagnostics
312
332
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`
314
334
*`--pretty` Use pretty diagnostic formatter <br/>*Default:*`false` <br/>*Environment:*`TS_NODE_PRETTY`
*`--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`
324
344
*`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.*
325
345
*`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.*
327
348
328
349
## API
329
350
@@ -403,7 +424,7 @@ You must set [`"type": "module"`](https://nodejs.org/api/packages.html#packages_
403
424
## Understanding configuration
404
425
405
426
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
407
428
`tsc --showConfig` but includes `"ts-node"` options as well.
408
429
409
430
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
415
436
node v16.1.0
416
437
compiler v4.2.2
417
438
418
-
$ ts-node --show-config
439
+
$ ts-node --showConfig
419
440
{
420
441
"compilerOptions": {
421
442
"target": "es6",
@@ -490,7 +511,7 @@ These tricks will make ts-node faster.
490
511
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.
491
512
492
513
* Enable [`transpileOnly`](#options) to skip typechecking
493
-
* Use our [`swc` integration](#bundled-swc-integration)
514
+
* Use our [`swc` integration](#swc)
494
515
* This is by far the fastest option
495
516
496
517
## With typechecking
@@ -519,12 +540,20 @@ Vanilla `node` loads `.js` by reading code from disk and executing it. Our hook
519
540
520
541
### Skipping `node_modules`
521
542
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:
523
544
524
545
1. Modules should always be published in a format node.js can consume
525
546
2. Transpiling the entire dependency tree will make your project slower
526
547
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
527
548
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
+
528
557
## paths and baseUrl

529
558
530
559
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
628
657
}
629
658
```
630
659
631
-
## Third-party transpilers
660
+
## Transpilers
632
661
633
662
In transpile-only mode, we skip typechecking to speed up execution time. You can go a step further and use a
634
663
third-party transpiler to transform TypeScript into JavaScript even faster. You will still benefit from
@@ -641,12 +670,11 @@ boilerplate.
641
670
> For our purposes, a compiler implements TypeScript's API and can perform typechecking.
642
671
> A third-party transpiler does not. Both transform TypeScript into JavaScript.
643
672
644
-
### Bundled `swc` integration
673
+
### swc
645
674
646
-
We have bundled an experimental `swc`integration.
675
+
swc support is built-in via the `--swc`flag or `"swc":true` tsconfig option.
647
676
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`.
650
678
651
679
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`.
652
680
@@ -659,20 +687,38 @@ Then add the following to your `tsconfig.json`.
0 commit comments