Originally published by ganeshmani009 at cloudnweb.dev
ECMAScript is a Javascript Standardization which gets updated every year, it is a good practice to update our code as well. ES6 Features
Sometimes, the browser doesn’t compatible with the latest javascript standards.
To solve this kind of problem, we need something like a babel which is nothing but a transpiler for javascript.
Firstly, we need to install two main packages to setup babel in the project.
Mainly, the reason for using the babel is to make use of Javascript new features in the codebase. we don’t know whether the node.js in the server will understand the particular code or not unless it is a vanilla javascript.
So, it is always recommended to transpile the code before deployment. there are two kinds of babel transpiling code.
$ npm init --yes $ npm install --save exress body-parser cors $npm install --save nodemon
Here, we initialize the package.json and install the basic express server with nodemon.
Next, we need to install @babel/core and @babel/node packages.
$ npm install @babel/core @babel/node --save-dev
After that, we need to create a file called .babelrc which contains all the babel configuration.
{ "presets": [ "@babel/preset-env" ] }
Now, the setup is ready. we need to create a script which transpile our code on run time.
"scripts": { "dev": "nodemon --exec babel-node index.js" }
Finally, add the following code in the index.js and run the script.
import express from 'express'; import bodyParser from 'body-parser';const app = express();
app.use(bodyParser.json());
app.get(‘/’,(req,res) => {
res.send(“Hello Babel”)
})app.listen(4000,() => {
console.log(app is listening to port 4000
);
})
$ npm run dev
Finally, you will see the output like Hello Babel.
Mainly, we cannot transpile the code in run time in production. So, what we need to do is, to compile the code into vanilla javascript and deploy the compiled version to the node.js production server.
Add the following command for building a compiled version of code
“scripts”: {
“build” : “babel index.js --out-file index-compiled.js”,
“dev”: “nodemon --exec babel-node index.js”
}
Babel compiles the index.js file and writes the compiled version in index-compiled.js
Meanwhile, when you run the command, you will get the compiled version like
Finally, we need to deploy the compiled version in the node.js production server.
To Learn Babel in-depth, you can refer to this video from Brad Traversy
Originally published by ganeshmani009 at cloudnweb.dev
===========================================
Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter
☞ Svelte.js - The Complete Guide
☞ The Complete JavaScript Course 2019: Build Real Projects!
☞ Become a JavaScript developer - Learn (React, Node,Angular)
☞ JavaScript: Understanding the Weird Parts
☞ JavaScript: Coding Challenges Bootcamp - 2019
☞ The Complete Node.js Developer Course (3rd Edition)
☞ Angular & NodeJS - The MEAN Stack Guide
☞ NodeJS - The Complete Guide (incl. MVC, REST APIs, GraphQL)
☞ Node.js Absolute Beginners Guide - Learn Node From Scratch
#node-js #javascript #express #web-development