Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldn't babel-node be used in production?

Tags:

babeljs

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.

like image 961
callum Avatar asked Jul 08 '16 10:07

callum


People also ask

Should I use Babel Node?

If you've been active as a Node. js developer, or even dabbled in front-end libraries like React or Vue. js, then there's no doubt that you've likely run across Babel.

Why do we need Babel Node?

Remember that Babel is a popular tool that lets you use the newest features of JavaScript. And many frameworks today use Babel under the hood to compile their code. For example, Node can't use ES6 import and export statements and some other cool features of ES6 syntax without the help of a compiler like Babel.


1 Answers

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.

like image 66
KeatsPeeks Avatar answered Sep 20 '22 21:09

KeatsPeeks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!