42

Does anyone know which target/libs are required for Node.js v10.x to use the built in async/await without the generators? I see a lot for node 8 but not with node 10.

Linus Unnebäck
  • 23,234
  • 15
  • 74
  • 89
four43
  • 1,675
  • 2
  • 20
  • 33
  • 1
    Possible duplicate of [How do I find the latest Typescript target support for any version of node?](https://stackoverflow.com/questions/54012303/how-do-i-find-the-latest-typescript-target-support-for-any-version-of-node) – Evan Carroll Jan 03 '19 at 01:49

2 Answers2

91

As of Node.js 10.0.0, 100% of ES2018 is supported. If you know that you are targeting that version or newer, the optimal config would look like this:

  • "module": "commonjs"

    Node.js is on its way to add ES-Modules, but for now we'll have to stick with CommonJS.

  • "target": "es2018"

    This tells TypeScript that it's okay to output JavaScript syntax with features from ES2018. In practice, this means that it will e.g. output object rest/spread properties & async/await syntax instead of embedding a polyfill.

  • "lib": ["es2018"]

    This tells TypeScript that it's okay to use functions and properties introduced in ES2018 or earlier. In practice, this means that you can use e.g. Promise.prototype.finally, Array.prototype.includes and String.prototype.padStart.

The full config would thus be:

{
  "compilerOptions": {
    "lib": ["es2018"],
    "module": "commonjs",
    "target": "es2018"
  }
}

If you are running Node.js 18 you can see my similar answer for Node.js 18 here

If you are running Node.js 16 you can see my similar answer for Node.js 16 here

If you are running Node.js 14 you can see my similar answer for Node.js 14 here

If you are running Node.js 12 you can see my similar answer for Node.js 12 here

If you are running Node.js 8 you can see my similar answer for Node.js 8 here

Linus Unnebäck
  • 23,234
  • 15
  • 74
  • 89
  • Why not `"lib": ["esnext"]`? You want to be able to use the latest features when writing code. Any features that don't exist in the `target` will be transformed by the typescript compiler to work on that target. – V Maharajh Jan 14 '20 at 18:08
  • 2
    @VivekMaharajh TypeScript doesn't add polyfills for e.g. `Promise.prototype.finally`, so if you use `"lib": ["esnext"]` you might accidentally use functions that aren't available in Node.js 10. With the posted answer, you will still be able to take advantage of all new _language features_, just not new _library functions_, since they won't be available on your target. – Linus Unnebäck Jan 15 '20 at 10:34
  • Oh, good to know. I always assumed that the typescript compiler would fail if it couldn't transpile the source code into the target. – V Maharajh Jan 15 '20 at 21:38
  • Did anything change for node 12? – Tom Jan 16 '20 at 22:04
  • 1
    @Tom Yes it did, see here: https://stackoverflow.com/a/59787575/148072 – Linus Unnebäck Jan 17 '20 at 12:42
  • Thanks for the explanation @LinusUnnebäck ! Just to be clear: A language feature would be something like arrow syntax vs. a library function is anything related to the standard lib such as Promise.allSettled()? – tjeisenschenk Jan 12 '21 at 15:23
  • I wouldn't use the phrase "language feature" to make that distinction, as it is ambiguous to me in this context. I think @LinusUnnebäck is spot on in his answer, writing about _syntax_ vs _standard library_ ("functions and properties"). – Simon Alling Feb 18 '21 at 08:12
7

According to recommended typescript config for node 8 , --target ES2017 is supported on Node 8.10.0 and newer (which would include Node 10), and it is sufficient to pass through async functions to the output without translating them to generators.

Matt McCutchen
  • 28,856
  • 2
  • 68
  • 75
  • 13
    Node 10 supports 100% of ES2018 features so you can target that https://node.green/#ES2018 – Hendry Sep 28 '18 at 14:16