39

Does node.js support a let statement something like what's described on MDN??

var x = 8,
    y = 12;

let ( x = 5, y = 10) {
    return x + y;
} //15

If not, is there a way to duplicate the functionality with a self-executing anonymous function or something?

And/or is there another js environment that

  1. has let and and
  2. has a REPL, as node does? Rhino?

EDIT:

This question was asked quite a while ago. As of now, late 2015, the answer is "Yes, yes it does". Harmony features were included by default in io.js 3.3, and have been recently brought back to node.js with the 4.x release.

ben author
  • 2,855
  • 2
  • 25
  • 43

5 Answers5

84

Yes, you can use let within node.js, however you have to run node using the optional --harmony flag. Try the following test.js:

"use strict"
var x = 8,
    y = 12;

{ let x = 5, y = 10; console.log(x + y); }

console.log(x + y);

And then run the file node --harmony test.js which results in:

15
20

I would not recommend using this in an important production application, but the functionality is available now.

Timothy Strimple
  • 22,920
  • 6
  • 69
  • 76
34

This is an old question and the accepted answer is no longer correct.

let support was added in Node.js 4.x.

See here for the full version support matrix.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
20
node --use_strict --harmony_scoping
Shamaoke
  • 6,222
  • 8
  • 34
  • 40
  • This answer applies for actual versions of Node as simple `--harmony` option doesn't work with Node v0.12.4 – Fr0sT Jun 25 '15 at 17:24
  • What about when installing a module with `npm install`? Is there a `npm --use_strict --harmony_scoping`? – Aaron Franke Dec 16 '19 at 02:57
7

I don’t think Node supports let, but you can do this:

var a = 5;

(function () {
  var a = 6;
  console.log(a); // => 6
})();

console.log(a); // => 5
Todd Yandell
  • 14,656
  • 2
  • 50
  • 37
  • 6
    That's about what I figured. It just reads poorly. I want `let`! *temper tantrum* – ben author Jul 01 '12 at 16:56
  • I wonder your snippet proves anything. By creating anonymous function, you created a new scope anyway. – rda3mon Feb 03 '13 at 01:48
  • 2
    THANK YOU THANK YOU!!! Perfect fix for my problem using objects inside a loop that have callbacks on them. (object gets overwritten before callback does its stuff) Kudos to you my friend. – James Sefton Nov 30 '13 at 03:37
  • 8
    This answer was accepted as a pragmatic solution for 2012, but 'let' is now supported by V8 and Node. – ben author Sep 18 '15 at 12:07
  • What about if the problem is in a module I'm using and not my own code? I can't install `ejs` because it contains `let`. – Aaron Franke Dec 16 '19 at 02:55
1

You can use the Babel transpiler and use let as well as many other ES6/ES2015 features.

To use babel:

$ npm install --save-dev babel

Then in your package.json:

"scripts": {
  "start": "babel-node index.js"
}

Inside index.js:

let foo  = 'bar;

Then start the server:

$ npm start
heartyporridge
  • 1,151
  • 8
  • 25
Johnny Oshika
  • 54,741
  • 40
  • 181
  • 275