Is it okay to use babel-node in production Is it okay to use babel-node in production javascript javascript

Is it okay to use babel-node in production


For the client side code, you're doing the correct thing. babelify it and ship it to the client.


For the server side code, I would just do a regular build using babel-cli

According to http://babeljs.io/docs/setup/#babel_register, babel-register is not meant for production use — The require hook is primarily recommended for simple cases.

for Babel 6+

As of Babel 6, no transformations are included by default. So let's start by installing babel-cli and babel-preset-es2015.

$ npm install --save-dev babel-cli babel-preset-es2015

Add a transformation to your .babelrc file — this is the prest module we downloaded above. Take a look at the full list of presets to see which one(s) are a best fit for you.

{  "presets": ["es2015"]}

Add a build script to your package.json. Below src is your input files and build is the transformed output files

"scripts": {  "build": "babel src -d build"}

Then build it!

$ npm run build

Then run your code. At this point, you'll want to be executing the files in your build directory

$ npm start

for Babel <= 5, just use the require hook.

require("babel/register");

All subsequent files required by node with the extensions .es6, .es, .jsx and .js will be transformed by Babel. The polyfill is also automatically required.

You will be able to keep your source files in ES6 but still execute them using node server.js


According to your comments, you seem to be having a little trouble. Pay particular attention to the yellow highlighted part above. Your first file can only be ES5, which is run by node itself. All subsequent requires will be transformed by Babel...

Here's what a typical setup might look like

server.js

// only ES5 is allowed in this filerequire("babel/register");// other babel configuration, if necessary// load your appvar app = require("./app.js");

app.js

// this file will be loaded through babel// you can now use ES6 here and in every other include

fire it up!

$ node server.js


I just wrote a blog post on this topic

Babeljs CLI documentation warns the following:

babel-node not meant for production use

You should not be using babel-node in production. It is unnecessarily heavy, with high memory usage due to the cache being stored in memory. You will also always experience a startup performance penalty as the entire app needs to be compiled on the fly.

This is an example of how you could set up the npm scripts to run your app with node instead of babel-node.

"scripts": {  "clean": "rm -rf build && mkdir build",  "build-css": "node-sass scss/app.scss public/css/app.css",  "build-server": "babel -d ./build ./server -s",  "build": "npm run clean && npm run build-css && npm run build-server",  "lint": "eslint source/ --quiet",  "start": "node ./build/index.js",  "debug": "node --debug ./build/index.js",  "test": "for i in $(ls tests/); do babel-node \"./tests/${i}\" | faucet ; done",  "validate": "npm run lint; npm run test && npm outdated --depth 0"},

You can find more details on the blog post


It's important to weigh the pros and cons of using babel-node in production.

  • babel-node does add between half a second to one second to the startup cost, on commodity hardware. But if your app is a long-running server, that startup cost won't matter much.
  • Try to measure the extra memory consumption. For my app for example (reading and processing time series data), it was only 20MB. Depending on your situation, this may or may not be significant.

On the other hand,

  • using babel-node directly simplifies development - you won't need "build" scripts, and you won't have separate src/lib and dist directories
  • if you import from local files, will you import from src/myutils, or from lib/myutils? Using babel-node eliminates that problem.

I only use Babel for modules support. Now V8 just released support for modules on January 10, 2017. Hopefully we'll see modules support in Node under a flag in a few months, rendering my reason for using Babel moot.