3

Is there any way to run ES6 in Node REPL (Read Evaluate Print Loop)? While running ES 6 commands, I am getting error as shown in the screenshot. Appreciate if someone can help me to configure Node to run ES6 code.

enter image description here

Prasad Honrao
  • 211
  • 3
  • 18
  • 4
    You are probably using a very old version of Node. Latest versions support nearly all of the ES6 features. Upgrade. – Ram Jul 24 '16 at 16:27
  • 1
    What version of node do you use? What does `node -v` or `console.log(process.version)` show? – t.niese Jul 24 '16 at 16:28
  • 1
    Latest versions of node by default shipped with ES6 support. http://node.green/ – lokesh-coder Jul 24 '16 at 16:45

3 Answers3

4

TL;DR: Upgrade Node

I have Node.js v6.0.0, which means I have all the ES6 features unlocked by default. My REPL has support for (basically) everything. Now, node v6.0.0 is currently in development, so you might not want to upgrade your production server, but if you're a developer, it's really stable enough for everyday use.

If you must use an outdated version of node, I would suggest you install n. It's a way to manage your versions of node on one machine.

Good Luck!

bren
  • 4,176
  • 3
  • 28
  • 43
1

Node 4 and higher supports most ES6 features out of the box, here is compatibility table.

To use ES6 features in older Node versions, it should be started with --harmony flag, and code should run in strict mode.

It is not possible to enable strict mode with 'use strict' for REPL globally, so ES6 code should be placed inside IIFE.

Strict mode can be enabled globally with --use_strict. To enable experimental ES6 support in REPL in older Node versions (0.12.x and lower) it should be started with

node --harmony --use_strict
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
-1

Add 'use strict'.

Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a "strict" operating context. This strict context prevents certain actions from being taken and throws more exceptions.

more information about use strict is here or here

enter image description here

Community
  • 1
  • 1
Aikon Mogwai
  • 4,954
  • 2
  • 18
  • 31
  • 1
    Instead of writing `I must write more 30 symbols to post comment` you could have explained why adding `'use strict'` will help. – t.niese Jul 24 '16 at 16:44
  • Fixed. I think that it's is obviously :( I often wonder that some people don't use strict mode. – Aikon Mogwai Jul 24 '16 at 16:48
  • 3
    I don't see anything in those links about `use strict` enabling the use of `let` in ECMAScript 5. It will just make `let` a reserved word and throw an error if you use it as an identifier. – JohnnyHK Jul 24 '16 at 17:00
  • 1
    If it is obvious then the OP would have known it. Beside that your quote only contains that it prevents certain things. Why should an _instruction_ that prevents certain actions from being taken, add more features? The `'use strict'` is a new feature of ES5, so some vendors decided to required the use of `'use strict'` to activate the ES5 and above features. But this is nothing related to the `'use strict'` instruction itself, but only a thing the vendors decided to make the transition between older and newer code easier. In newer engines it is not needed anymore. – t.niese Jul 24 '16 at 17:00