PM2 and Babel

I recently dove into the land of Babel to take advantage of the latest and greatest that javascript has to offer. I’ve been using Babel as an interpreter on the client-side via jspm, which I’m a huge fan of. However, using it on the server side just seemed more difficult. But, it wasn’t too bad. Babel has instructions for a lot of setups and situations.

I’m using nodemon in dev and pm2 in production. There are instructions for nodemon, but not pm2, so that’s what I’m going to explain.

Install babel on your production server

1
$ npm install -g babel

That was easy. Now we can globally use babel-node instead of node to execute our es6+ code.

Tell pm2 to use babel

I first looked a this GH issue. There was a lot of confusion. All of the confusion revolves around what command line command to give. I’m using a JSON config file, so I thought I would just consult the JSON config file docs. There is an option called “exec_interpreter”. That looks pretty good. I’ll add that to my JSON file.

Here it is in context:

1
2
3
4
5
6
7
8
{
"name" : "myapp",
"script" : "/var/www/myapp/current/bin/www",
"exec_interpreter" : "babel-node",
"env": {
"NODE_ENV": "production"
}
}

Based on the confusion in the thread I wasn’t sure if that would work, but it did. You’ll have to to kill the old PM2 process and start up from the JSON file again.

One interesting side-effect was that my memory usage (that PM2 reports) dropped over 60%. I don’t know why, but I’m not complaining.

The first answer to that GH issue was to “use bable-node as the exec_interpreter” and that was 100% the answer. Perhaps this is a case of RTFM? I’m usually on the wrong side of that, so I’m glad it worked out!

avatar

Dev Blog