Using let and Other ES 6 Additions in Node
Problem
You want to use some of the new ECMAScript 6 functionality, such as let in your Node
application, but they don’t seem to work.
Solution
You’ll need to use two command-line options when you run the Node application:
harmony, to add in support for whatever ECMAScript Harmony features are currently
implemented, and use-strict to enforce strict JavaScript processing:
node --harmony --use-strict open.js
Or you can trigger strict mode by adding the following line as the first in the application:
'use strict';
EXPLAIN
Internally, Node runs on V8, Google’s open source JavaScript engine. You might assume
that the engine implements most if not all of the newest cutting-edge JavaScript func‐
tionality, including support for let.
Once the --harmony option has been given, you can use let instead of var. However,
you must also use strict mode to use let, either by providing the command-line flag, or
using use strict in the application:And it is true that Google has implemented much
of the newest JavaScript functionality.
However, some of the newer functionality isn’t available to a Node application unless
you specify the harmony command-line option, similar to having to turn the option on
in your browser. You can find this and other options by typing the following at the
command line:
'use strict'; let fs = require('fs'); fs.readFile('main.txt', {encoding: 'utf8', flag: 'r+'},function(err,data) { if (err) { console.log(err.message); } else { console.log(data); } });
No comments:
Post a Comment