62

What is the recommended config for typescript if I want to ue the compiled sources with node 8?

most tutorials use the following tsconig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs"
  }
}

But now I figured out, that not all available features are supported. For example ['foo'].includes('bar') throws the error: Property 'includes' does not exist on type 'string[]'.

I found an issue that addresses this problem. The solution is to use the lib es7. I could overwrite the default libs: "lib": ["es7"]

But I'm not sure if this is the best config for node 8 - are there more features which are not supported by that lib? are there to much features defined?

So my question is: What are the best configurations for target, lib and module if I want to use node 8?

Xenya
  • 1,255
  • 2
  • 11
  • 13

3 Answers3

115

As of Node.js 8.10.0, 100% of ES2017 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 it's way to add ES-Modules, but for now we'll have to stick with CommonJS.

  • "target": "es2017"

    This tells TypeScript that it's okay to output JavaScript syntax with features from ES2017. In practice, this means that it will e.g. output async/await instead of embedding a polyfill (__awaiter).

  • "lib": ["es2017"]

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

The full config would thus be:

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

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 10 you can see my similar answer for Node.js 10 here

Linus Unnebäck
  • 23,234
  • 15
  • 74
  • 89
  • 1
    I'm trying to find something confirming that 8.10.0+ supports 100% of ES2017. Kangax's compatibility table only shows 8.9.x so it's outdated. The node changelog doesn't make any claim about ES2017 compatibility. Do you have a source for this? – eyelidlessness May 29 '18 at 18:34
  • 4
    https://node.green/ is an updated version of the Kangax table, specifically tailored for Node.js versions, that's what I was looking at – Linus Unnebäck May 29 '18 at 21:46
  • 6
    Wow, I looked at node.green and would never have discovered that, because it's buried under the 9.11.1 header. I wish there was a way to specify the versions shown in the table. Thanks for your help. – eyelidlessness May 30 '18 at 00:36
  • I'm not sure but shouldn't the noLib option be specified for node? – cquezel Aug 28 '19 at 22:03
  • is Promise.finally supported in ES2017? Because it isn't by Node 8 – velop Nov 27 '19 at 10:06
  • 1
    @velop `Promise.finally` is a part of ES2018, not ES2017. It's supported by Node.js 10.0.0 and newer. If you are running Node.js 10, see this answer: https://stackoverflow.com/a/57607634/148072 – Linus Unnebäck Nov 28 '19 at 11:29
5

I'm sure you've already found this but there is Microsoft's starter template here: https://github.com/Microsoft/TypeScript-Node-Starter

Whilst you are still on Node 8.x, keep your module set to commonjs, target can be es6.

compilerOptions.lib only defines what declarations the compiler uses for compile time checks, it does not affect the output of tsc. In other words, you can use whatever lib you want and not worry that your transpiled code will be any different (this is controlled entirely by compilerOptions.target).

Using es7 as a lib in your case will be fine and will give you type declarations for ES7 and under.

Array.includes is ES7 (ES2016) and therefore as you've discovered is not part of ES6. You could define your lib as; lib: ["es6", "ES2016.Array.Include"] to get around your issue.

Robula
  • 649
  • 1
  • 12
  • 29
  • According to http://node.green/ you could target `es2016` safely in Node 8.9.4, you could probably get away with targeting `es2017` if you want to use native async/await. – Robula Jan 22 '18 at 12:06
4

There's a page in the TypeScript wiki which lists recommended TSConfig settings for various node versions.

panepeter
  • 3,224
  • 1
  • 29
  • 41
orta
  • 4,225
  • 1
  • 26
  • 34