Search code examples
javascriptnode.jsgoogle-chromev8specifications

SyntaxError in Chrome but ReferenceError in node


So I run the same code 5=6 on both chrome and node this of course produces an error. But look at the error type!

different errors on chrome vs node

On chrome it's a SyntaxError on node ReferenceError.

Well that's odd. For one thing cause both use v8, for another because well...you'd think this would be defined in the spec, so someone has to be wrong.

The same thing happens with strings. Same thing when running js outside of the chrome console as a script. Same thing when running outside of the node console as a script.

Someone suggested that it might be due to differences in boxing since Number(5) = 6 is a ReferenceError on both but that's just the case for any fn() = _. And if that is the case, then it still doesn't explain why the behavior is different in the two engines.

So what's the correct (as per the spec) thing to do here? Who is wrong and why are the two engines giving me different errors?


Solution

  • The ECMAScript language specification changed in June 2019 so that many things that were previously a ReferenceError (including attempting to assign to a number literal) are now a SyntaxError.

    v8 implemented this change (also in June 2019) in version 7.7.196, which was included in Chrome 77 and Node 12.11. Prior to those versions, attempting to assign to a number literal resulted in a ReferenceError in both Node and Chrome. Starting with those versions, the same code now results in a SyntaxError.

    $ ./node-v12.10.0 -e 5=6 2>&1 | grep Error
    ReferenceError: Invalid left-hand side in assignment
    $ ./node-v12.11.0 -e 5=6 2>&1 | grep Error
    SyntaxError: Invalid left-hand side in assignment