12

The babel-node docs carry a stern warning:

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.

Let's break this down:

  • Memory usage – huh? All modules are 'cached' in memory for the lifetime of your application anyway. What are they getting at here?

  • Startup penalty – how is this a performance problem? Deploying a web app already takes several seconds (or minutes if you're testing in CI). Adding half a second to startup means nothing. In fact if startup time matters anywhere, it matters more in development than production. If you're restarting your web server frequently enough that the startup time is an issue, you've got much bigger problems.

Also, there is no such warning about using Babel's require hook (require('babel-register')) in production, even though this presumably does pretty much exactly the same thing as babel-node. For example, you can do node -r babel-register server.js and get the same behaviour as babel-node server.js. (My company does exactly this in hundreds of microservices, with no problems.)

Is Babel's warning just FUD, or am I missing something? And if the warning is valid, why doesn't it also apply to the Babel require hook?


Related: Is it okay to use babel-node in production – but that question just asks if production use is recommended, and the answers just quote the official advice, i.e. "No". In contrast, I am questioning the reasoning behind the official advice.

Community
  • 1
  • 1
callum
  • 34,206
  • 35
  • 106
  • 163
  • While there is no warning in the require hook itself, there is a warning in the require hook's documentation (Pick "Require hook" in the "Choose your tool" table in https://babeljs.io/docs/setup/). – Jessidhia Aug 26 '16 at 11:57

2 Answers2

2

babel-node

The production warning was added to resolve this issue :

Without the kexec module, you can get into a really ugly situation where the child_process dies but its death or error never bubbles up. For more info see https://github.com/babel/babel/issues/2137.

It would be great if the docs on babel-node explained that it is not aimed for production and that without kexec installed that it has bad behaviour.

(emphasis mine)

The link for the original issue #2137 is dead, but you can find it here.

So there seems to be two problems here :

  • "very high memory usage on large apps"
  • "without kexec installed that it has bad behaviour"

These problems lead to the production warning.

babel-register

Also, there is no such warning about using Babel's require hook (require('babel-register')) in production

There may be no warning but it is not recommanded either. See this issue :

babel-register is primarily recommended for simple cases. If you're running into issues with it, it seems like changing your workflow to one built around a file watcher would be ideal. Note that we also never recommend babel-register for production cases.

Community
  • 1
  • 1
KeatsPeeks
  • 19,126
  • 5
  • 52
  • 83
1

I don't know enough about babel's and node's internals to give a full answer; some of this is speculation, but the caching babel-node would do is not the same thing as the cache node does.

babel-node's cache would be another cache on top of node's require cache, and it would have to, at best, cache the resulting source code (before it's fed to node).

I believe node's cache, after evaluating a module, will only cache things reachable from the exports, or, rather, the things that are not reachable anymore will be eventually GCed.

The startup penalty will depend on the contents of your .babelrc, but you're forcing babel to do the legwork to translate your entire source code every time it is executed. Even if you implement a persistent cache, babel-node would still need to do a cache fetch and validation for each file of your app.

In development, more appropriate tools like webpack in watch mode can, after the cold start, re-translate only modified files, which would be much faster than even a babel-node with perfectly optimized cache.

Jessidhia
  • 514
  • 4
  • 7
  • _"babel-node's cache would be another cache on top of node's require cache"_ What exactly is this extra cache, and what is its purpose? And why doesn't babel-register have the same issue? – callum Aug 26 '16 at 11:57
  • Turns out both babel-node and babel-register also use a disk cache in "your temporary directory", wherever that is: https://babeljs.io/docs/usage/require/ – Jessidhia Aug 26 '16 at 12:16