UNPKG

35.8 kBMarkdownView Raw
1<!--
2BEFORE EDITING THIS README
3Our README.md is auto-generated by combining pages in website/docs and website/readme-sources
4
5If you are sending a pull request to improve documentation, submit your changes
6in the source markdown files and we will generate the README from there.
7
8You can build the readme with this command:
9
10 cd website && yarn build-readme
11-->
12
13# ![TypeScript Node](logo.svg?sanitize=true)
14
15[![NPM version](https://img.shields.io/npm/v/ts-node.svg?style=flat)](https://npmjs.org/package/ts-node)
16[![NPM downloads](https://img.shields.io/npm/dm/ts-node.svg?style=flat)](https://npmjs.org/package/ts-node)
17[![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)
18[![Test coverage](https://codecov.io/gh/TypeStrong/ts-node/branch/main/graph/badge.svg)](https://codecov.io/gh/TypeStrong/ts-node)
19
20> TypeScript execution and REPL for node.js, with source map and native ESM support.
21
22The latest documentation can also be found on our website: <https://typestrong.org/ts-node>
23
24# Table of Contents
25
26* [Overview](#overview)
27 * [Features](#features)
28* [Installation](#installation)
29* [Usage](#usage)
30 * [Shell](#shell)
31 * [Shebang](#shebang)
32 * [Programmatic](#programmatic)
33 * [Developers](#developers)
34* [Configuration](#configuration)
35 * [CLI flags](#cli-flags)
36 * [Via tsconfig.json (recommended)](#via-tsconfigjson-recommended)
37 * [@tsconfig/bases](#tsconfigbases)
38 * [Default config](#default-config)
39 * [`node` flags](#node-flags)
40* [Options](#options)
41 * [Shell](#shell-1)
42 * [TSConfig](#tsconfig)
43 * [Typechecking](#typechecking)
44 * [Transpilation](#transpilation)
45 * [Diagnostics](#diagnostics)
46 * [Advanced](#advanced)
47 * [API](#api)
48* [CommonJS vs native ECMAScript modules](#commonjs-vs-native-ecmascript-modules)
49 * [CommonJS](#commonjs)
50 * [Native ECMAScript modules](#native-ecmascript-modules)
51* [Troubleshooting](#troubleshooting)
52 * [Understanding configuration](#understanding-configuration)
53 * [Understanding Errors](#understanding-errors)
54 * [`TSError`](#tserror)
55 * [`SyntaxError`](#syntaxerror)
56 * [Unsupported JavaScript syntax](#unsupported-javascript-syntax)
57* [Make it fast](#make-it-fast)
58 * [Skip typechecking](#skip-typechecking)
59 * [With typechecking](#with-typechecking)
60* [Advanced](#advanced-1)
61 * [How It Works](#how-it-works)
62 * [Skipping `node_modules`](#skipping-node_modules)
63 * [paths and baseUrl
64 ](#paths-and-baseurl)
65 * [Why is this not built-in to ts-node?](#why-is-this-not-built-in-to-ts-node)
66 * [Help! My Types Are Missing!](#help-my-types-are-missing)
67 * [Third-party compilers](#third-party-compilers)
68 * [Third-party transpilers](#third-party-transpilers)
69 * [Bundled `swc` integration](#bundled-swc-integration)
70 * [Writing your own integration](#writing-your-own-integration)
71 * [Module type overrides](#module-type-overrides)
72 * [Caveats](#caveats)
73* [Recipes](#recipes)
74 * [Watching and Restarting](#watching-and-restarting)
75 * [AVA](#ava)
76 * [CommonJS](#commonjs-1)
77 * [Native ECMAScript modules](#native-ecmascript-modules-1)
78 * [Gulp](#gulp)
79 * [IntelliJ and Webstorm](#intellij-and-webstorm)
80 * [Mocha](#mocha)
81 * [Mocha 7 and newer](#mocha-7-and-newer)
82 * [Mocha <=6](#mocha-6)
83 * [Tape](#tape)
84 * [Visual Studio Code](#visual-studio-code)
85 * [Other](#other)
86* [License](#license)
87
88# Overview
89
90ts-node is a TypeScript execution engine and REPL for Node.js.
91
92It JIT transforms TypeScript into JavaScript, enabling you to directly execute TypeScript on Node.js without precompiling.
93This is accomplished by hooking node's module loading APIs, enabling it to be used seamlessly alongside other Node.js
94tools and libraries.
95
96## Features
97
98* Automatic sourcemaps in stack traces
99* Automatic `tsconfig.json` parsing
100* Automatic defaults to match your node version
101* Typechecking (optional)
102* REPL
103* Write standalone scripts
104* Native ESM loader
105* Use third-party transpilers
106* Use custom transformers
107* Integrate with test runners, debuggers, and CLI tools
108* Compatible with pre-compilation for production
109
110![TypeScript REPL](website/static/img/screenshot.png)
111
112# Installation
113
114```shell
115# Locally in your project.
116npm install -D typescript
117npm install -D ts-node
118
119# Or globally with TypeScript.
120npm install -g typescript
121npm install -g ts-node
122
123# Depending on configuration, you may also need these
124npm install -D tslib @types/node
125```
126
127**Tip:** Installing modules locally allows you to control and share the versions through `package.json`. TS Node will always resolve the compiler from `cwd` before checking relative to its own installation.
128
129# Usage
130
131## Shell
132
133```shell
134# Execute a script as `node` + `tsc`.
135ts-node script.ts
136
137# Starts a TypeScript REPL.
138ts-node
139
140# Execute code with TypeScript.
141ts-node -e 'console.log("Hello, world!")'
142
143# Execute, and print, code with TypeScript.
144ts-node -p -e '"Hello, world!"'
145
146# Pipe scripts to execute with TypeScript.
147echo 'console.log("Hello, world!")' | ts-node
148
149# Equivalent to ts-node --transpile-only
150ts-node-transpile-only script.ts
151
152# Equivalent to ts-node --cwd-mode
153ts-node-cwd script.ts
154```
155
156## Shebang
157
158```typescript
159#!/usr/bin/env ts-node
160
161console.log("Hello, world!")
162```
163
164Passing CLI arguments via shebang is allowed on Mac but not Linux. For example, the following will fail on Linux:
165
166 #!/usr/bin/env ts-node --files
167 // This shebang is not portable. It only works on Mac
168
169Instead, specify all ts-node options in your `tsconfig.json`.
170
171## Programmatic
172
173You 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.
174
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.
176
177### Developers
178
179ts-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`.
180
181# Configuration
182
183ts-node supports a variety of options which can be specified via `tsconfig.json`, as CLI flags, as environment variables, or programmatically.
184
185For a complete list, see [Options](#options).
186
187## CLI flags
188
189ts-node CLI flags must come *before* the entrypoint script. For example:
190
191```shell
192$ ts-node --project tsconfig-dev.json say-hello.ts Ronald
193Hello, Ronald!
194```
195
196## Via tsconfig.json (recommended)
197
198ts-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
200Use `--skip-project` to skip loading the `tsconfig.json`. Use `--project` to explicitly specify the path to a `tsconfig.json`.
201
202When 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()`.
203
204You can use this sample configuration as a starting point:
205
206```jsonc title="tsconfig.json"
207{
208 // This is an alias to @tsconfig/node12: https://github.com/tsconfig/bases
209 "extends": "ts-node/node12/tsconfig.json",
210
211 // Most ts-node options can be specified here using their programmatic names.
212 "ts-node": {
213 // It is faster to skip typechecking.
214 // Remove if you want ts-node to do typechecking.
215 "transpileOnly": true,
216
217 "files": true,
218
219 "compilerOptions": {
220 // compilerOptions specified here will override those declared below,
221 // but *only* in ts-node. Useful if you want ts-node and tsc to use
222 // different options with a single tsconfig.json.
223 }
224 },
225 "compilerOptions": {
226 // typescript options here
227 }
228}
229```
230
231Our bundled [JSON schema](https://unpkg.com/browse/ts-node@latest/tsconfig.schema.json) lists all compatible options.
232
233### @tsconfig/bases
234
235[@tsconfig/bases](https://github.com/tsconfig/bases) maintains recommended configurations for several node versions.
236As a convenience, these are bundled with ts-node.
237
238```jsonc title="tsconfig.json"
239{
240 "extends": "ts-node/node16/tsconfig.json",
241
242 // Or install directly with `npm i -D @tsconfig/node16`
243 "extends": "@tsconfig/node16/tsconfig.json",
244}
245```
246
247### Default config
248
249If no `tsconfig.json` is loaded from disk, ts-node will use the newest recommended defaults from
250[@tsconfig/bases](https://github.com/tsconfig/bases/) compatible with your `node` and `typescript` versions.
251With the latest `node` and `typescript`, this is [`@tsconfig/node16`](https://github.com/tsconfig/bases/blob/master/bases/node16.json).
252
253Older versions of `typescript` are incompatible with `@tsconfig/node16`. In those cases we will use an older default configuration.
254
255When in doubt, `ts-node --show-config` will log the configuration being used, and `ts-node -vv` will log `node` and `typescript` versions.
256
257## `node` flags
258
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`
260
261We recommend using the [`NODE_OPTIONS`](https://nodejs.org/api/cli.html#cli_node_options_options) environment variable to pass options to `node`.
262
263```shell
264NODE_OPTIONS='--trace-deprecation --abort-on-uncaught-exception' ts-node ./index.ts
265```
266
267Alternatively, you can invoke `node` directly and install ts-node via `--require`/`-r`
268
269```shell
270node --trace-deprecation --abort-on-uncaught-exception -r ts-node/register ./index.ts
271```
272
273# Options
274
275`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
277*Environment variables, where available, are in `ALL_CAPS`*
278
279## Shell
280
281* `-h, --help` Prints the help text
282* `-v, --version` Prints the version. `-vv` prints node and typescript compiler versions, too
283* `-e, --eval` Evaluate code
284* `-p, --print` Print result of `--eval`
285* `-i, --interactive` Opens the REPL even if stdin does not appear to be a terminal
286
287## TSConfig
288
289* `-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
294
295## Typechecking
296
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`
300* `--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`
302
303## Transpilation
304
305* `-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`
307* `-C, --compiler [name]` Specify a custom TypeScript compiler <br/>*Default:* `typescript` <br/>*Environment:* `TS_NODE_COMPILER`
308* `--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`
310
311## Diagnostics
312
313* `--log-error` Logs TypeScript errors to stderr instead of throwing exceptions <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_LOG_ERROR`
314* `--pretty` Use pretty diagnostic formatter <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_PRETTY`
315* `TS_NODE_DEBUG` Enable debug logging<br/>
316
317## Advanced
318
319* `-r, --require [path]` Require a node module before execution
320* `--cwd` Behave as if invoked in this working directory <br/>*Default:* `process.cwd()`<br/>*Environment:* `TS_NODE_CWD`
321* `--emit` Emit output files into `.ts-node` directory <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_EMIT`
322* `--scope` Scope compiler to files within `scopeDir`. Anything outside this directory is ignored. <br/>\*Default: `false` <br/>*Environment:* `TS_NODE_SCOPE`
323* `--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* `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* `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
327
328## API
329
330The API includes [additional options](https://typestrong.org/ts-node/api/interfaces/RegisterOptions.html) not shown here.
331
332# CommonJS vs native ECMAScript modules
333
334TypeScript 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.
335
336Here is a brief comparison of the two.
337
338| CommonJS | Native ECMAScript modules |
339|---|---|
340| Write native `import` syntax | Write native `import` syntax |
341| Transforms `import` into `require()` | Does not transform `import` |
342| 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) |
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` |
344
345## CommonJS
346
347Transforming to CommonJS is typically simpler and more widely supported because it is older. You must remove [`"type": "module"`](https://nodejs.org/api/packages.html#packages_type) from `package.json` and set [`"module": "CommonJS"`](https://www.typescriptlang.org/tsconfig/#module) in `tsconfig.json`.
348
349```jsonc title="package.json"
350{
351 // This can be omitted; commonjs is the default
352 "type": "commonjs"
353}
354```
355
356```jsonc title="tsconfig.json"
357{
358 "compilerOptions": {
359 "module": "CommonJS"
360 }
361}
362```
363
364If you must keep `"module": "ESNext"` for `tsc`, webpack, or another build tool, you can set an override for ts-node.
365
366```jsonc title="tsconfig.json"
367{
368 "compilerOptions": {
369 "module": "ESNext"
370 },
371 "ts-node": {
372 "compilerOptions": {
373 "module": "CommonJS"
374 }
375 }
376}
377```
378
379## Native ECMAScript modules
380
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.
382
383For complete usage, limitations, and to provide feedback, see [#1007](https://github.com/TypeStrong/ts-node/issues/1007).
384
385You must set [`"type": "module"`](https://nodejs.org/api/packages.html#packages_type) in `package.json` and [`"module": "ESNext"`](https://www.typescriptlang.org/tsconfig/#module) in `tsconfig.json`.
386
387```jsonc title="package.json"
388{
389 "type": "module"
390}
391```
392
393```jsonc title="tsconfig.json"
394{
395 "compilerOptions": {
396 "module": "ESNext" // or ES2015, ES2020
397 }
398}
399```
400
401# Troubleshooting
402
403## Understanding configuration
404
405ts-node uses sensible default configurations to reduce boilerplate while still respecting `tsconfig.json` if you
406have one. If you are unsure which configuration is used, you can log it with `ts-node --show-config`. This is similar to
407`tsc --showConfig` but includes `"ts-node"` options as well.
408
409ts-node also respects your locally-installed `typescript` version, but global installations fallback to the globally-installed
410`typescript`. If you are unsure which versions are used, `ts-node -vv` will log them.
411
412```shell
413$ ts-node -vv
414ts-node v10.0.0
415node v16.1.0
416compiler v4.2.2
417
418$ ts-node --show-config
419{
420 "compilerOptions": {
421 "target": "es6",
422 "lib": [
423 "es6",
424 "dom"
425 ],
426 "rootDir": "./src",
427 "outDir": "./.ts-node",
428 "module": "commonjs",
429 "moduleResolution": "node",
430 "strict": true,
431 "declaration": false,
432 "sourceMap": true,
433 "inlineSources": true,
434 "types": [
435 "node"
436 ],
437 "stripInternal": true,
438 "incremental": true,
439 "skipLibCheck": true,
440 "importsNotUsedAsValues": "error",
441 "inlineSourceMap": false,
442 "noEmit": false
443 },
444 "ts-node": {
445 "cwd": "/d/project",
446 "projectSearchDir": "/d/project",
447 "require": [],
448 "project": "/d/project/tsconfig.json"
449 }
450}
451```
452
453## Understanding Errors
454
455It 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.
456
457### `TSError`
458
459Type errors from the compiler are thrown as a `TSError`. These are the same as errors you get from `tsc`.
460
461### `SyntaxError`
462
463Any 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.
464
465#### Unsupported JavaScript syntax
466
467Your version of `node` may not support all JavaScript syntax supported by TypeScript. The compiler must transform this syntax via "downleveling," which is controlled by
468the [tsconfig `"target"` option](https://www.typescriptlang.org/tsconfig#target). Otherwise your code will compile fine, but node will throw a `SyntaxError`.
469
470For example, `node` 12 does not understand the `?.` optional chaining operator. If you use `"target": "esnext"`, then the following TypeScript syntax:
471
472```typescript
473const bar: string | undefined = foo?.bar;
474```
475
476will compile into this JavaScript:
477
478```javascript
479const a = foo?.bar;
480```
481
482When you try to run this code, node 12 will throw a `SyntaxError`. To fix this, you must switch to `"target": "es2019"` or lower so TypeScript transforms `?.` into something `node` can understand.
483
484# Make it fast
485
486These tricks will make ts-node faster.
487
488## Skip typechecking
489
490It 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
492* Enable [`transpileOnly`](#options) to skip typechecking
493* Use our [`swc` integration](#bundled-swc-integration)
494 * This is by far the fastest option
495
496## With typechecking
497
498* Avoid dynamic `require()` which may trigger repeated typechecking; prefer `import`
499* Try with and without `--files`; one may be faster depending on your project
500* Check `tsc --showConfig`; make sure all executed files are included
501* Enable [`skipLibCheck`](https://www.typescriptlang.org/tsconfig#skipLibCheck)
502* Set a [`types`](https://www.typescriptlang.org/tsconfig#types) array to avoid loading unnecessary `@types`
503
504# Advanced
505
506## How It Works
507
508ts-node works by registering hooks for `.ts`, `.tsx`, `.js`, and/or `.jsx` extensions.
509
510Vanilla `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`.
511
512`.js` and `.jsx` are only transformed when [`allowJs`](https://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options) is enabled.
513
514`.tsx` and `.jsx` are only transformed when [`jsx`](https://www.typescriptlang.org/docs/handbook/jsx.html) is enabled.
515
516> **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.
517
518> **Warning:** When ts-node is used with `allowJs`, all non-ignored JavaScript files are transformed using the TypeScript compiler.
519
520### Skipping `node_modules`
521
522By default, **TypeScript Node** avoids compiling files in `/node_modules/` for three reasons:
523
5241. Modules should always be published in a format node.js can consume
5252. Transpiling the entire dependency tree will make your project slower
5263. 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
528## paths and baseUrl&#xA;
529
530You 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`.
531
532```jsonc title="tsconfig.json"
533{
534 "ts-node": {
535 // Do not forget to `npm i -D tsconfig-paths`
536 "require": ["tsconfig-paths/register"]
537 }
538}
539```
540
541### Why is this not built-in to ts-node?
542
543The 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).
544
545> The TypeScript compiler has a set of additional flags to *inform* the compiler of transformations that are expected to happen to the sources to generate the final output.
546>
547> 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.
548
549This means `"paths"` are intended to describe mappings that the build tool or runtime *already* performs, not to tell the build tool or
550runtime 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.
551
552## Help! My Types Are Missing!
553
554ts-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.
555
556For 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).
557
558Example `tsconfig.json`:
559
560```jsonc
561{
562 "compilerOptions": {
563 "typeRoots" : ["./node_modules/@types", "./typings"]
564 }
565}
566```
567
568Example project structure:
569
570```text
571<project_root>/
572-- tsconfig.json
573-- typings/
574 -- <module_name>/
575 -- index.d.ts
576```
577
578Example module declaration file:
579
580```typescript
581declare module '<module_name>' {
582 // module definitions go here
583}
584```
585
586For module definitions, you can use [`paths`](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping):
587
588```jsonc
589{
590 "compilerOptions": {
591 "baseUrl": ".",
592 "paths": {
593 "custom-module-type": ["types/custom-module-type"]
594 }
595 }
596}
597```
598
599An 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:
600
601```typescript
602/// <reference types="./types/untyped_js_lib" />
603import UntypedJsLib from "untyped_js_lib"
604```
605
606**Tip:** If you *must* use `files`, `include`, or `exclude`, enable `--files` flags or set `TS_NODE_FILES=true`.
607
608## Third-party compilers
609
610Some projects require a patched typescript compiler which adds additional features. For example, [`ttypescript`](https://github.com/cevek/ttypescript/tree/master/packages/ttypescript) and [`ts-patch`](https://github.com/nonara/ts-patch#readme)
611add the ability to configure custom transformers. These are drop-in replacements for the vanilla `typescript` module and
612implement the same API.
613
614For example, to use `ttypescript` and `ts-transformer-keys`, add this to your `tsconfig.json`:
615
616```jsonc title="tsconfig.json"
617{
618 "ts-node": {
619 // This can be omitted when using ts-patch
620 "compiler": "ttypescript"
621 },
622 "compilerOptions": {
623 // plugin configuration is the same for both ts-patch and ttypescript
624 "plugins": [
625 { "transform": "ts-transformer-keys/transformer" }
626 ]
627 }
628}
629```
630
631## Third-party transpilers
632
633In transpile-only mode, we skip typechecking to speed up execution time. You can go a step further and use a
634third-party transpiler to transform TypeScript into JavaScript even faster. You will still benefit from
635ts-node's automatic `tsconfig.json` discovery, sourcemap support, and global ts-node CLI. Integrations
636can automatically derive an appropriate configuration from your existing `tsconfig.json` which simplifies project
637boilerplate.
638
639> **What is the difference between a compiler and a transpiler?**
640>
641> For our purposes, a compiler implements TypeScript's API and can perform typechecking.
642> A third-party transpiler does not. Both transform TypeScript into JavaScript.
643
644### Bundled `swc` integration
645
646We have bundled an experimental `swc` integration.
647
648[`swc`](https://swc.rs) is a TypeScript-compatible transpiler implemented in Rust. This makes it an order of magnitude faster
649than `transpileModule`.
650
651To use it, first install `@swc/core` or `@swc/wasm`. If using `importHelpers`, also install `@swc/helpers`.
652
653```shell
654npm i -D @swc/core @swc/helpers
655```
656
657Then add the following to your `tsconfig.json`.
658
659```jsonc title="tsconfig.json"
660{
661 "ts-node": {
662 "transpileOnly": true,
663 "transpiler": "ts-node/transpilers/swc-experimental"
664 }
665}
666```
667
668> `swc` uses `@swc/helpers` instead of `tslib`. If you have enabled `importHelpers`, you must also install `@swc/helpers`.
669
670### Writing your own integration
671
672To write your own transpiler integration, check our [API docs](https://typestrong.org/ts-node/api/interfaces/TranspilerModule.html).
673
674Integrations 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.
676
677## Module type overrides
678
679When deciding between CommonJS and native ECMAScript modules, ts-node defaults to matching vanilla `node` and `tsc`
680behavior. This means TypeScript files are transformed according to your `tsconfig.json` `"module"` option and executed
681according to node's rules for the `package.json` `"type"` field.
682
683In some projects you may need to override this behavior for some files. For example, in a webpack
684project, you may have `package.json` configured with `"type": "module"` and `tsconfig.json` with
685`"module": "esnext"`. However, webpack uses our CommonJS hook to execute your `webpack.config.ts`,
686so you need to force your webpack config and any supporting scripts to execute as CommonJS.
687
688In these situations, our `moduleTypes` option lets you override certain files, forcing execution as
689CommonJS or ESM. Node supports similar overriding via `.cjs` and `.mjs` file extensions, but `.ts` files cannot use them.
690`moduleTypes` achieves the same effect, and *also* overrides your `tsconfig.json` `"module"` config appropriately.
691
692The following example tells ts-node to execute a webpack config as CommonJS:
693
694```jsonc title=tsconfig.json
695{
696 "ts-node": {
697 "transpileOnly": true,
698 "moduleTypes": {
699 "webpack.config.ts": "cjs",
700 // Globs are also supported with the same behavior as tsconfig "include"
701 "webpack-config-scripts/**/*": "cjs"
702 }
703 },
704 "compilerOptions": {
705 "module": "es2020",
706 "target": "es2020"
707 }
708}
709```
710
711Each key is a glob pattern with the same syntax as tsconfig's `"include"` array.
712When multiple patterns match the same file, the last pattern takes precedence.
713
714* `cjs` overrides matches files to compile and execute as CommonJS.
715* `esm` overrides matches files to compile and execute as native ECMAScript modules.
716* `package` resets either of the above to default behavior, which obeys `package.json` `"type"` and `tsconfig.json` `"module"` options.
717
718### Caveats
719
720Files with an overridden module type are transformed with the same limitations as [`isolatedModules`](https://www.typescriptlang.org/tsconfig#isolatedModules). This will only affect rare cases such as using `const enum`s with [`preserveConstEnums`](https://www.typescriptlang.org/tsconfig#preserveConstEnums) disabled.
721
722This feature is meant to facilitate scenarios where normal `compilerOptions` and `package.json` configuration is not possible. For example, a `webpack.config.ts` cannot be given its own `package.json` to override `"type"`. Wherever possible you should favor using traditional `package.json` and `tsconfig.json` configurations.
723
724# Recipes
725
726## Watching and Restarting
727
728**TypeScript Node** compiles source code via `require()`, watching files and code reloads are out of scope for the project. If you want to restart the `ts-node` process on file change, existing node.js tools such as [nodemon](https://github.com/remy/nodemon), [onchange](https://github.com/Qard/onchange) and [node-dev](https://github.com/fgnass/node-dev) work.
729
730There's also [`ts-node-dev`](https://github.com/whitecolor/ts-node-dev), a modified version of [`node-dev`](https://github.com/fgnass/node-dev) using `ts-node` for compilation that will restart the process on file change.
731
732## AVA
733
734Assuming you are configuring AVA via your `package.json`, add one of the following configurations.
735
736### CommonJS
737
738Use this configuration if your `package.json` does not have `"type": "module"`.
739
740```jsonc title"package.json"
741{
742 "ava": {
743 "extensions": [
744 "ts"
745 ],
746 "require": [
747 "ts-node/register"
748 ]
749 }
750}
751```
752
753### Native ECMAScript modules
754
755This configuration is necessary if your `package.json` has `"type": "module"`.
756
757```jsonc title"package.json"
758{
759 "ava": {
760 "extensions": {
761 "ts": "module"
762 },
763 "nonSemVerExperiments": {
764 "configurableModuleFormat": true
765 },
766 "nodeArguments": [
767 "--loader=ts-node/esm"
768 ]
769 }
770}
771```
772
773## Gulp
774
775ts-node support is built-in to gulp.
776
777```sh
778# Create a `gulpfile.ts` and run `gulp`.
779gulp
780```
781
782See also: https://gulpjs.com/docs/en/getting-started/javascript-and-gulpfiles#transpilation
783
784## IntelliJ and Webstorm
785
786Create a new Node.js configuration and add `-r ts-node/register` to "Node parameters."
787
788**Note:** If you are using the `--project <tsconfig.json>` command line argument as per the [Configuration Options](#configuration), and want to apply this same behavior when launching in IntelliJ, specify under "Environment Variables": `TS_NODE_PROJECT=<tsconfig.json>`.
789
790## Mocha
791
792### Mocha 7 and newer
793
794```shell
795mocha --require ts-node/register --extensions ts,tsx --watch --watch-files src 'tests/**/*.{ts,tsx}' [...args]
796```
797
798Or specify options via your mocha config file.
799
800```jsonc title=".mocharc.json"
801{
802 // Specify "require" for CommonJS
803 "require": "ts-node/register",
804 // Specify "loader" for native ESM
805 "loader": "ts-node/esm",
806 "extensions": ["ts", "tsx"],
807 "spec": [
808 "tests/**/*.spec.*"
809 ],
810 "watch-files": [
811 "src"
812 ]
813}
814```
815
816See also: https://mochajs.org/#configuring-mocha-nodejs
817
818### Mocha <=6
819
820```shell
821mocha --require ts-node/register --watch-extensions ts,tsx "test/**/*.{ts,tsx}" [...args]
822```
823
824**Note:** `--watch-extensions` is only used in `--watch` mode.
825
826## Tape
827
828```shell
829ts-node node_modules/tape/bin/tape [...args]
830```
831
832## Visual Studio Code
833
834Create 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`).
835
836```jsonc title=".vscode/launch.json"
837{
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 }],
850}
851```
852
853**Note:** If you are using the `--project <tsconfig.json>` command line argument as per the [Configuration Options](#configuration), and want to apply this same behavior when launching in VS Code, add an "env" key into the launch configuration: `"env": { "TS_NODE_PROJECT": "<tsconfig.json>" }`.
854
855## Other
856
857In many cases, setting [`NODE_OPTIONS`](https://nodejs.org/api/cli.html#cli_node_options_options) will enable `ts-node` within other node tools, child processes, and worker threads.
858
859```shell
860NODE_OPTIONS="-r ts-node/register"
861```
862
863Or, if you require native ESM support:
864
865```shell
866NODE_OPTIONS="--loader ts-node/esm"
867```
868
869This tells any node processes which receive this environment variable to install `ts-node`'s hooks before executing other code.
870
871# License
872
873ts-node is licensed under the MIT license. [MIT](https://github.com/TypeStrong/ts-node/blob/main/LICENSE)
874
875ts-node includes source code from Node.js which is licensed under the MIT license. [Node.js license information](https://raw.githubusercontent.com/nodejs/node/master/LICENSE)
876
877ts-node includes source code from the TypeScript compiler which is licensed under the Apache License 2.0. [TypeScript license information](https://github.com/microsoft/TypeScript/blob/master/LICENSE.txt)