I have been playing 1,2 with some ES6 code, but always via Babel or Traceur. Yet I miss having code running straight from its source, without any large dev dependencies or transpiling steps.
Can we use some of the ES6 features right now on the most popular versions of Node (0.12 and 4)?
Turns out we can, and it is pretty simple. It is so simple, that we can probably avoid very long discussions on how to publish ES6 modules, at least for today.
The main idea is:
Just start using the basic ES6 features, like
constand arrow functions in your code. Node 4 and 5 will play nicely right away, and Node 0.12 will handle it just fine if you add
--harmonyto the CLI shebang line.
That is it. Here is the experiment. Let us take a few features that I like the most
let, arrow functions, string templates, default arguments,
Object.assign and the new string methods (like
Do they work under Node 0.12 and 4? Well, some do by default in v4 and even work in v0.12
I write a lot of NodeJS CLI applications, so adding a flag to the shebang line is no problem. For example, a quick NPM script runner npm-quick-run has the following shebang in its bin script
#!/usr/bin/env node --harmony 'use strict' const quickRun = require('..') let help = [ 'USE: nrun <npm script prefix> <script arguments>', '\t"nr t" === "nrun t" === "npm test"', '\t"nr m -w" probably will find "npm run mocha -- --w"' ].join('\n')
This code runs as expected under Node 0.12; even better, the
--harmony command line argument
is silently removed by the Node runtime. If we print the arguments, it is not there.
$ node --harmony -e "console.log(process.argv)" "foo" [ 'node', 'foo' ]
So all code that worked before adding
--harmony should just continue working. We do need
to set the strict mode, otherwise Node 0.12 cannot parse the
Testing available features
To figure out what can be used, I plainly ran a few code samples. If they compile and execute - we are in business! The following tests worked right away
ES6 features that are not working in Node 0.12
Testing the other ES6 features are tricky. First, one cannot even test the template literals directly -
the code would not even compile. One has to resort to all sorts of tricks
eval. But by running pieces of code manually, I have found that the features
I wanted the most where not available. Here is the short list with possible work arounds I use
- template literals - _.template
- Object.assign - _.assign
- default parameters - _.defaults
- import/export - no alternative except to transpile
As you can see, pretty much everything might be just a library call away, except the Lodash templates do NOT support multi line strings like template literals; and of course no ES6 modules.
Using these little ES6 gems makes me more productive. And the same code can work in the browser - one needs to transpile it anyway!
What about other CLI tools that I do not control, but often use, like Mocha?
Well, when I run an NPM script that calls a local Mocha install, I can use
--harmony flag too,
see the Mocha bin script.
Finally, a shameless plug: whenever I need to lookup ES6 syntax or semantics, I use manpm CLI tool.
- Some people had problem running shebang with
--harmonyat the end - for example Ubuntu systems.