JavaScript Introducing ES2015 Classes Structure of Class

Why is the same code throwing syntax errors in terminal /Mac ?

Running the same code in terminal, throws the following errors: constructor({ name, age, interestLevel = 5 } = {}) { ^

SyntaxError: Unexpected token { at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:373:25) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Function.Module.runMain (module.js:441:10) at startup (node.js:139:18) at node.js:974:3

Is there anything wrong with my version of node (4.6.1)? Will these lessons become more useful when I get to use a framework ?

1 Answer

andren
andren
28,360 Points

The error is likely caused by the version of node you are using. Node did not get full support (well 99% support) for ES2015 until version 6.

You can see a list of ES2015 features that various versions of Node supports on this page. According to that page version 4 only supported about 57% of the ES2015 feature set, and destructuring is one of the features not supported at all.

So you should upgrade to at least the newest LTS release (Version 6.11.1) if you plan on using ES2015 features.