How to Setup Babel in Node.js

Node.js is one of the most popular back-end technologies out there now. It's friendly, strong, and well maintained, and doesn't go anywhere shortly.

To help you learn how to use it effectively, in this article we will create a simple server using a node with babel which is configured in our code.

But before we do dive in to build our server, let's learn more about what babel is.

What is babel?

babel is a javascript compiler. This is a popular tool that helps you use the latest features of the JavaScript programming language.

Why use babel in Node.js?

Have you ever opened a repo back end built with node.js / Express - and the first thing you see is an ES6 import and export statement along with some other cool ES6 syntax features?

Well, babel makes it all possible. Remember that a babel is a popular tool that allows you to use the latest Javascript features. And many of today's frameworks use babel under the hood to compile their code.

For example, nodes cannot use import reports and export ES6 and some other cool features of ES6 syntax without the help of compilers such as babel.

So in this tutorial, I will show you how to set your node application quickly to be compatible with most ES6 syntax.

Amazing right?

This guideline assumes you have the following elements:

Basic knowledge of node.js

Node installed on your machine

Code or text editor of your choice

Start

Let's set the basic node application that we will use for this tutorial.

Create a new folder. For this tutorial, I will call Mine Node-babel. Now add a folder to the workspace, and open your terminal.

Let's initialize and create package files.json for our application:

NPM Init.

This command will display a few steps of the settings we want to leave for what they are. So press the Enter button or return in all settings it will function properly.

After it's finished, create a new file called "index.js" that will function as our entry point.

How to set and install babel

Now, we will install three packages from the babel family:

NPM packages for @ Babel / Cli, @ Babel / Core, and @ Babel / Preset-Env Install

To install, we use the command below to install the package:

—Save-dev @ Babel / CLI @ Babel / Core @ Babel / preset-env const server =

We want to use --Save-dev to install it as a dependency for the development of your module. So after you are done with the installation, create a new file called.BelRC to configure the babel.

Touch .BelRC.

This file will host all the options we want to add to babel. So for now, let's use the settings that I usually use for development in my application. You can copy it and add you:

{
"Preset": [
    ["@ Babel / Env", {
      	"Target": {{
        "Node": "Current"
      	}
    }]
  	By,
  	"Plugin": [
	    "@ babel / plugin-proposal-class-class",
	    "@ Babel / Plugin-Proposal-Object-Spread"
  	]
}

The above configuration is what I use to tell babel that yes, I want to use my import and export statements and the class features and the rest and the spread of operators from ES6.

It's amazing? Let's continue.

How to set a simple server

Now open the "index.js" file that we created before, and add this code to generate a simple server:

HTTP imports from 'HTTP';

http.createserver ((req, res) => http.createserver ((req, res) => http.createserver ((req, res) => http.createserver ((req, res) => http.createserver ((req
	res .en ('hello from the server');
}). Listen (4001);

console.log ('server up and running);

Default server export;

With the sample code on the server, we will listen to port 4001 and then send the "Halo of the server" response to us every time we visit the port.

Package.json script configuration.

We now have a simple server. To run this, we have to translate our code before walking with a node. To do this, open the "Package.json" file and add Build and Start Script:

"Scripts": {
+ "Build": "babel Index.js -d Dist",
    "Start": "NPM Run Build && Node Dist / Index.js"
}

our server up and operating with this command:

NPM starts

You must get this response after you visit Localhost: 4001

How to use Nodemon to keep an eye on your server and restart it

To prevent restart the server itself every time you make changes to your application, we need to install Nodemon. You can install Nodemon to your application using this command to install it as Dev Dependency:

NPM Install --Save-dev nodemon

And then we reconfigure our package package.json:

"Scripts": {
   "Build": "babel Index.js -d Dist",
   "Start": "NPM Run Build && Nodemon Dist / Index.js"
}

It's amazing, now this is the final code of our node application and what you have to get when you run "NPM Start" to start your server.

As you can see from the picture above, our server goes up and running. You can now use import and export statements from ES6 syntax and other extraordinary features provided by ES6 as the rest and the operator deployment in your node application.

Conclusion

In this tutorial, we have learned how to use extraordinary ES6 syntax in our node application using babel.

Note that you can add more configuration in your .belrc file. This is not limited to what we have in this tutorial - so don't hesitate to change or change it.