What version of Javascript is supported in node.js

Javascriptnode.jsV8

Javascript Problem Overview


I'm getting started with Node.js and I'm having a hard time figuring out what version of JavaScript is supported by node which makes it difficult figuring out what features I can use. Here's what I know.

  • Node uses V8
  • V8 implements ECMAScript as specified in ECMA-262, 3rd edition
  • ECMA-262, 3rd edition is JavaScript 1.5

Given this, I would assume I can use JavaScript 1.5 compatible code in node. However, it turns out I can use the Array.forEach, among other constructs, even though according to MDC it isn't available until Javascript 1.6 -- ECMA-262, 5th edition.

Where am I going wrong? Is there a document somewhere that details the available language features?

Javascript Solutions


Solution 1 - Javascript

This matrix (V8 follows the WebKit column fairly closely) seems to pretty well answer the question "what features can I use?" but I can't find a canonical answer to "what version of javascript is supported?" As far as I can tell, the best answer is this: ECMA-262 3rd edition is supported, but many features of the 5th edition are also supported.

There's a good explanation of why V8 follows the WebKit and JavaScriptCore functionality on this thread.

Solution 2 - Javascript

The Node javascript version depends on which version of v8 that Node uses. Node version 0.5.1 (14 Jul 2011) and upwards use v8 3.4.10 or later, and are 5th edition ECMA-262, rather than 3rd Edition. 5th edition is equivalent to Javascript 1.8.5. See below reasons.

Between 21 May 2011 and 15 June 2011 the v8 website stopped listing v8 as implementing 3rd edition ECMA-262 and started showing 5th edition. http://web.archive.org/web/20110521053630/http://code.google.com/p/v8/ http://web.archive.org/web/20110615102350/http://code.google.com/p/v8/

According to the v8 changelog, on the 15 June 2011 v8 changed to version 3.4.4. So that version and later are 5th edition.

According to the Node changelog, v8 3.4.10 was in Node Version 0.5.1, 14 Jul 2011, so that version and later are 5th edition ECMA-26. This does not mean that v8 versions before 3.4.10 were purely 3rd edition though, since there may have been a steady transition from 3rd to 5th through many v8 versions.

Solution 3 - Javascript

Looks like, at some point, node.green was created to track JavaScript feature support against different Node versions.

Solution 4 - Javascript

It seems as though we have been reduced to two strategies to figure out which version of Javascript node uses:

Strategy 1: trust in what some document somewhere says, which is wrong in many cases. I haven't found the table which indicates the key-value pairs of which version of node supports which version of ECMAScript.

Strategy 2: guess-and-check.

Find a feature quoted by ES6 and "see if it fails", something like this:

el@apollo:~/code$ echo "console.log('blue'.includes('blue'))" > a.js
el@apollo:~/code$ cat a.js 
console.log('blue'.includes('blue'))
el@apollo:~/code$ node a.js
/home/el/code/javascript/02/a.js:1
ports, require, module, __filename, __dirname) { console.log('blue'.includes('
                                                                    ^
TypeError: undefined is not a function
    at Object.<anonymous> (/home/el/code/javascript/02/a.js:1:82)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3
el@apollo:~/code$ babel-node a.js
true

I suppose the "babel" here is a reference to the Tanakh. What features does our version of node support? Well, I don't know, you'll have to manually test it. This is going to become a giant MESS. And worse, it seems to be on purpose.

Solution 5 - Javascript

Here is a 2016 blog post from the V8 team which explains why this is a mess, especially since 2016 when TC39 started releasing new JavaScript / ECMAScript specifications more frequently.

If you are using Visual Studio Code (or any editor using Language Server / LSP) then valid values for the compilation target include such things as es2015 (aka es6) and es2016.

The only workaround I can find to know if my version of Node supports a given version of JavaScript goes like this:

  1. Find the V8 version in that particular Node either by running node -p process.versions.v8, or via the Node docs
  2. Find which version of Chrome corresponds to that V8 version, via Wikipedia
  3. Go to the Kangax compatibility table mentioned in the V8 blog post above, and check which features / JavaScript versions that version of Chrome supports

The result is that currently Node LTS (16.13.1), includes V8 9.4, which supports all the ECMAScript 2022 features.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionemtraneView Question on Stackoverflow
Solution 1 - JavascriptBensonView Answer on Stackoverflow
Solution 2 - JavascriptJohn MiddlemasView Answer on Stackoverflow
Solution 3 - JavascriptNate WhittakerView Answer on Stackoverflow
Solution 4 - JavascriptEric LeschinskiView Answer on Stackoverflow
Solution 5 - JavascriptDerek HillView Answer on Stackoverflow