Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to use `require('babel/register);` in production

It should not use babel-node in production according to the official document of babel. Some said you can use babel/register instead of babel-node if you don't want to compile your ES6 code into ES5 before running. But doesn't babel-node use babel/register internally ? What is the difference between babel-node and require('babel/register'); . Is it okay using require('babel/register'); in production ?

like image 300
爱国者 Avatar asked Nov 13 '15 13:11

爱国者


People also ask

Is babel needed in production?

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.

What is the use of babel register?

The purpose of babel is to transpile your js current code to an understandable version of js for the given environment, tool, framework you're using.

What is babel register NPM?

By default @babel/node cli and @babel/register will save to a json cache in your temporary directory. This will heavily improve with the startup and compilation of your files. There are however scenarios where you want to change this behaviour and there are environment variables exposed to allow you to do this.


1 Answers

The Babel Handbook (linked from babeljs.io) says that it's bad practice to use babel-register in production (see babel-register user guide):

Note that this is not meant for production use. It's considered bad practice to deploy code that gets compiled this way. It is far better to compile ahead of time before deploying. However this works quite well for build scripts or other things that you run locally.

So it seems the suggested way is to compile before running using the babel command from the babel-cli package.

This also fits in with the npm advice on using transpilers. (That advice is given for authoring npm packages, but applies in this case too).

like image 188
Andrew Bryant Avatar answered Oct 12 '22 01:10

Andrew Bryant