Skip to content

Commit e4ed5e6

Browse files
authored
docs(README.md): merge my explanations into syntax error section and change title
1 parent 91eca37 commit e4ed5e6

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

README.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,18 @@ Most options can be specified by a `"ts-node"` object in `tsconfig.json` using t
221221

222222
Our bundled [JSON schema](https://unpkg.com/browse/ts-node@8.8.2/tsconfig.schema.json) lists all compatible options.
223223

224-
## SyntaxError
224+
## Help! I'm getting a `SyntaxError` in `ts-node`, but my code compiles with `tsc`!
225225

226-
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 runtime issues with your code.
226+
This happens when your `compileOptions.target` is too high, so TypeScript leaves some fancy new syntax as-is, instead of transpiling it into a form your version of Node understands.
227+
228+
For example, if you're using the `?.` optional chaining operator with `"target": "esnext"` or `"target": "es2020"`, `tsc` won't transpile it,
229+
and Node 12, which doesn't support `?.`, will throw a `SyntaxError`. If you lower `"target"` to `"es2019"`, `tsc` will transpile the `?.`
230+
into a form that works on Node 12.
231+
232+
To determine which target to use, see https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping.
233+
234+
Any error that is not a `TSError` is thrown while running the transpiled code in Node.js after compilation succeeds.
235+
If you see a `SyntaxError` or a `TypeError` from a builtin API being undefined, then you definitely need to lower your `target`.
227236

228237
### Import Statements
229238

@@ -289,16 +298,6 @@ import UntypedJsLib from "untyped_js_lib"
289298

290299
**Tip:** If you _must_ use `files`, `include`, or `exclude`, enable `--files` flags or set `TS_NODE_FILES=true`.
291300

292-
## Help! I'm getting syntax errors in `ts-node`, but not in `tsc`!
293-
294-
This is probably because the `compilerOptions.target` in your `tsconfig.json` is a higher version of ECMAScript than your current version of Node supports.
295-
296-
For example, if you're using the `?.` optional chaining operator, and you have `"target": "esnext"` or `"target": "es2020"`, the `?.` won't get transpiled at all,
297-
so if you're using Node 12, you'll get a syntax error because it doesn't natively support `?.`. If you lower `"target"` to `"es2019"`, it will transpile the `?.`
298-
into a form that can run on Node 12.
299-
300-
To determine which target to use, see https://github.com/microsoft/TypeScript/wiki/Node-Target-Mapping.
301-
302301
## Watching and Restarting
303302

304303
**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.

0 commit comments

Comments
 (0)