.Title

An unpleasant babeljs experiment

We all love new technologies, and we want to use them as soon as they’re out. ES6 comes with really nice features, for example:

Thing is, nodejs doesn’t allow the use of all those new, and mostly nice features, yet. This is why some folks build the well known Babeljs.

Yeah, this was my reaction too at first sight. It seems flawless. Building apps with a futuristic code, trying a lot of new features. The ES6 dream has come through! On top of that a nice-looking documentation!

So, like thousands of people I tried it, used it and over used it. I build a small project using the great import feature, ...arguments, templates and so on…

# The first caveat, publish…

When I wanted to publish my package to npm I hit a wall. I needed to share my pm2 module, on npm, and with tar packages.

If you want to do so, you have two solutions:

The most clean option is the first one, for example by using gulp-babel.

Actually it’s bad.

  1. You have to publish only the compiled files (either by changing the package.json, or by juggling with backup, src and dest .)
  2. npm publish is not easy to test without releasing new versions that you can’t erase: proof-npm-publish
  3. It’s actually a lot of code, headache, time for not so much
  4. What’s even worse is that stack traces won’t match perfectly to your nice and clean ES6 code. Remember, babel just compiles the damn stuff to javascript adding a lot of code:

proof-stack

# Let’s try to use child processes

Child processes are really easy to use with nodejs. With the power of IPC and the fork method you can do a lot of fun, nice and easy stuff. If you’ve a use case, use it.

I had a use case and, as I started with babel I wanted to enjoy experimental ES6 features!

To use babeljs inside a fork you have, again, two solutions:

I had trouble using the babel-node trick, as far as I can remember it was because of IPC.

So I used the require hook (as it was used in PM2). Thing is you’ll have to add babel to your dependencies. With a 24 Mb weight and 216 (not deduped) combined dependencies, it’s not light.

proof bash

# Alternative

Actually with node > 4.0 and the V8 update almost every interesting feature is now available! See the list of default supported es6 features. If you don’t yet, use 'use strict'; to support Block-scoped declarations like let, const, class. Or the --use_strict argument (not compatible with mocha).

The --harmony flag can be useful to enable es6 staged features like [].findIndex(), [].find(), 'abcde'.includes('cd') etc.

In my opinion this is the best choice. You can enjoy the good ES6 features without adding a whole engine on top of your app. Some features (like import/export or the rest parameter) are not available yet but will be in a near future! In the mean time use ES5 javascript for those things, you’ll be just as productive.

Babeljs is a nice tool to test features, just think twice before using it.