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 ?
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With