React JS Setup using Npm Babel and Webpack

React JS Setup using Npm Babel and Webpack:-

With the introduction of react JS  , the way we build our UI component has changed. things that we build our components in react so you must be aware that the learning curve of react is very small but there are some tools that we need to understand for going into react , so one of those tools are webpack and Babel.So in this article we will 
do React JS Setup using Npm Babel and Webpack.
 
We will not cover the full understanding of webpack in Babel ,  but we will see how we can use this in our react applications .




 
So guys we will be using Windows in our demonstration but you can use Linux as well so the prerequisite of starting this blog post is that you need to have node JS installed .
Let’s go to this URL and download node js. Please download the latest version of node js .
 
So once you have downloaded node JS with you have to go to command  prompt and verify by typing node -v 

 

So once you got the output as I have shown above that means your node JS is installed
Now navigate to any of the folder where you want to create your project and create a folder named as hello-world and initialize with npm .
We Will be using npm over here so , npm is a package manager for node based applications , similar to what is knwon as maven for java based applications.
 
  • We Will type npm init to initialize npm .
  • Will be getting one prompt so just accept the default for all the prompts whatever you get and continue.
  • npm init will create a package.json file in your working directory .
  • We Create One folder named as hello-world and then run npm init as shown below.

Configuring Babel :-

React Js Contains ES6 based code which most browsers doesn’t support at this point of time .Hence we need some tool to convert our ES6 code to plain vanilla javascript , and the tool is Babel . Let’s install all babel related dependencies one by one . Type in following commands one by one .

Now  install react related dependencies .

 

Configuring Webpack:-

Webpack is the bundler we will use, which will bundle all our javascript files.Its is basically a module bundler which bundles all our Javascript modules and assets into one or more bundles.

let’s install webpack using NPM

 

Install  webpack dev server , we use it for live reloading . It helps us to provide fast in memory access to assets bundled by webpack.

Now our webpack requires some config files let’s create a file named webpack.config.js

webpack.config.js:-

You Can see here , entry and output are mandatory to be provided in this config file .Our UI will run on port 8090 as configured in webpack.config.js  Lets go ahead and create a file known as main.js under hello-world directory.

main.js

Create A Basic  React Component :-

Create a file named Hello.jsx .

This is our basic component which just prints a basic html .You can see we are importing this in our main.js above

Create Index File:-

We are creating a file named as index.html.Our react JS transpiled minified code is added in index.html.

index.html

 

A important point to remember here is that we have referred index.js here , which is referred from memory .Webpack loads index.js into memory if you are running in dev mode.Update package.json to run webpack dev server as follow add  “start”: “webpack-dev-server –hot” under scripts property in package.json.

 

Running the Application:-

Type “npm start” , you get following output . If you face any issues kindly comment.

output:- 

 

npm start

Download Code

“Its Always Better To Learn and Share Instead Learn and Forget” 

Related Posts:-

MVC OR MVVM – Take Your Pick

Overload Or Override Static Methods in Java

5 thoughts on “React JS Setup using Npm Babel and Webpack

Leave a Reply

Your email address will not be published. Required fields are marked *