Now we know that what Babel is, we will focus on how to install it on your machine using node. Babel can be installed easily after following these simple steps.
Requirements :
- A code editor like atom, sublime text or Visual studio code.
- Node should be installed on the machine with npm too.
We will install Babel using Node. Open your text editor, then create your directories structure like the one below:
|--node_modules
|--src
--app.js
|--.babelrc
|--package.json
|--package.lock.json
If you know how node works then you know about node_modules, package.json, and package.lock.json. These are automatically formed once we run some commands.
Now, open the command line and set the path to the directory of the folder then write these lines in the cmd:
npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node
npm install nodemon --save-dev
The first npm commands will install babel dependencies and the second will is used to install nodemon which allows us to update the browser content without refreshing it.
After entering the command we will get:
As we can see in the above image, the command that we used to install babel dependencies are now visible in our 'package.json' file.
It is also important to add the below line inside the .babelrc file which we have in our project directory.
// .babelrc
{
"presets": ["@babel/preset-env"]
}
Now we finally need to add scripts into our 'package.json' file.
"start": "nodemon --exec babel-node src/app.js" // inside your scripts tag
The final 'package.json' will look like this:
Now we are all set we just need to write normal ES6, 7, 8 code in our app.js file and run it with 'npx babel filename' command where 'filename' is replaced by app.js here, and we will get the ES5 output in the console.
Example:
javascript
// next generation javascript code
let alice = () => {};
let bob = (b) => b;
const usingMap = [1, 2, 3].map((number) => number * 2);
console.log(usingMap); // [2, 4, 6]
var immukul = {
_name: "Mukul",
_friends: ["Mukul", "Mayank"],
printFriends(){
this._friends.forEach(
f =>console.log(this._name + " knows " + f));
}
};
console.log(immukul.printFriends());
Output:
Similar Reads
Language Translator using React Translator is a web application software tool that facilitate the translation of text from one language to another language using ReactJS. This application allows users to give input Text and select input and output language Format from the list of languages and uses Microsoft Translator API. Users
8 min read
ReactJS ES6 ES6, also known as ECMA script 2015 is a scripting language which is based on specification and standardization by ECMA International Company in ECMA-262 and ISO/IEC 1623. It is the sixth edition of the ECMAScript language specification standard. It was created to standardize JavaScript language to
6 min read
ReactJS Babel Introduction Babel is a JavaScript compiler that converts modern JavaScript code (like ES6+ and JSX) into a backwards-compatible version that older browsers can understand. In the context of React, Babel allows to use modern syntax like JSX and ES6+ features.Transpile ES6+ code: Convert modern JavaScript (ES6 an
5 min read
Currency converter app using ReactJS In this article, we will be building a very simple currency converter app with the help of an API. Our app contains three sections, one for taking the user input and storing it inside a state variable, a menu where users can change the units of conversion, and finally, a display section where we dis
4 min read
Shopping Cart app using React In this article, we will create an Interactive and Responsive Shopping Cart Project Application using the famous JavaScript FrontEnd library ReactJS. We have named or Application as "GeeksforGeeks Shopping Cart". The application or project which we have developed mainly focuses on the functional com
9 min read
ReactJS Examples This article contains a wide collection of React JS examples. These examples are categorized based on the topics, including components, props, hooks, and advanced topics in React. Many of these program examples contain multiple approaches to solve the respective problems.These React JS examples prov
5 min read