Last Updated: February 25, 2016
·
2.406K
· pornel

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.

2 Responses
Add your response

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 ·