1-liner to detect ES6 support (--harmony flag) in node js
Harmony added native Map
and Set
and node
is nice enough to hide them in ES5 mode:
if ('function' === typeof Map) /* yup, ES6 */
If you're writing project that uses new ES6 syntax you might want to add it somewhere at the top of your main file (before you load es6-shim
:)
if ('function' !== typeof Map) throw new Error("ES6 is required; add --harmony");
It's not a bullet-proof feature detection for all ES6 features, but a good enough and future-proof way to warn users when they launched node without required ES6 support.
Written by Kornel
Related protips
2 Responses
How about this one?
if (!~process.execArgv.indexOf('--harmony')) {
throw 'Node.js must be run with --harmony flag';
}
over 1 year ago
·
Checking command-line args doesn't seem future-proof to me. I expect ES6 to be on by default (without the flag) when it's ready to ship.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Node
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#