babel-register is a require hook, that will bind node’s require method and automatically transpile the file on the fly. This is not meant for production! It’s considered as a bad practice to compile the code this way. It’s far better to compile the code before deploying.
However this works quite well for development purposes.
Let’s install babel-register first:
npm install babel-register --save-dev
Create a simple index.js file:
Now create a sample.js file and require index.js and babel-register:
When you run the code using node sample.js you will see the output of index.js – “Hello World”.
Note: You can’t require (babel-register) in the same file that you want to compile, because Node is executing the file before Babel’s transpile.
Is it safe to use babel-register in production?