React Tutorials - Day Two - Environment Setup

This is the second article of this series, in previous articles we had a brief introduction to React and covered the required concepts of React like JSX, Virtual DOM and one way data binding. If you didn'tt read the previous article then I recommended you first read it.

Today we will learn how to set up a development environment for React. I discuss three different methods to setup a React project. These three methods are

  • Using the NPM
  • Using Visual Studio 2017 SPA templates
  • Using the Webpack and Babel

Let’s read about each method one by one. Before starting any method there will be npm installed in your system, without npm we can’t setup the React environment. So if npm is not installed in your system then first of all go to the Node.js official website and download and install the node.js . When you install the Node.js npm will be automatically installed in your system.

React

Using the NPM

Download React Global Packages

Now open your command line terminal and paste the following line of code and press enter.

“npm install -g create-react-app”

Above command install the global packages for React.

React

Create React App

After installing the React global packages now run “create-react-app reactapp” command, this command will create a React project and name this project as “reactapp”.

React

Run the Project:

After creating the project now change the directory and run “cd reactapp” command. Now you are in root directory of the project again run the “npm start” command this command run the React project on “3000” port.

React

In my case port number 3000 is used by another application so it runs the project on 3001 port. When you open the “localhost:3000” URL in your browser you will get following output.

React
If you are getting the above screen, then congratulations you successfully configure and run the React application. Don’t worry about the code written into the project code we will cover all the topics in the next article.

Using Visual Studio SPA Templates

If you are using Visual Studio 2017 then the easiest way to get started is by using one of the project templates which are made available. These plug into the standard dotnet new command and work on Windows and Mac in addition to Linux. To install the (Single Page Application)SPA templates, open the command prompt and run the command given below.

dotnet new --install Microsoft.AspNetCore.SpaTemplates::*

React

The command given above takes some time and installs several templates for SPA like Angular, Knockout.js, React etc. The benefit of all these templates are that we don’t need to worry about the whole setup process.

To generate a new project, create an empty folder and name this project as reactCore here. After creating an empty folder, now change the directory and go to this project. Now, run the command “dotnet new React“

React

This command creates a template project for React Application. Now, run “dotnet restore” commanpm nd. This command builds MSBuild file and restore all .NET dependencies.

React

After installing all the .NET dependencies, now we install Node.js dependencies. Run “npm install” command. This command takes a couple of minutes to install the required Node.js dependencies.

React

After creating the template project and installing all the required dependencies, now run “start reactCore.csproj” command. This command will open your project in Visual Studio.

React

React

Now, press Ctrl + F5 and run the project. When you run this project you will get the following screens.

React

React

Using Webpack and Babel

So far we set up the React Environment set up using the npm and Visual Studio 2017 SPA templates. Now we will focus on the Webpack and babel to setup the React project. I like this method and recommend this method among all three methods because it provides a full functionality to install the required packages and makes the configuration manually. If you see the project structure of the project created in Visual Studio 2017 then you will find that webpack and babel is also used there. So now we create the same project with manual configuration. Let’s start the setup.

First of all create an empty folder and name this folder as “reactwebpack”, after creating the folder now open this folder into any IDE; here I am using the Visual Studio Code. If you are new to Webpack then read this article. From this article you will get a brief intro about the webpack and learn what webpack is and how it works.

 

 

Step 1 Create Package.json file

Now open the command line terminal and run the “npm init” command. When you run this command it will ask several question, you don’t need to answer all the questions just press enter and skip the questions.

React

After completing this process you will find that a “package.json” file has been created in your project. Package.json is main configuration file of our project and contains the information of the packages uses in our project.

React

Step 2 Install the Required Libraries

Now we needs to install the required libraries to run and configure the application. Let’s install all these libraries.

Now run “npm install react react-dom --save” command; this command installs the react and react-dom packages into our project.

React

Now we install the webpack dependencies, for this run the “npm install webpack webpack-dev-server –save-dev” command.

React
Let’s install the react related packages. For this run the “npm install babel-core babel-loader babel-preset-react babel-preset-es2015 babel-preset-stage-2 --save-dev” command, this command install the several babel packages.

React

Here babel-core and babel-loader are main dependencies and other dependencies provide the set of rules to babel for the react development. Babel is the heart of any react application.

React

In React we use the JSX and write the JavaScript code into ES6 format and all the browsers don’t understand the ES6 format. So we need an intermediary that can convert the ES6 code to ES5 code. So babel runs the ES6 code in our browser.

React

After installing all the required packages, you will find that all the dependencies are configured in your package.json file.

Step 3 Configure the web server and loaders.

Now create a “webpack.config.js” file in root directory of the project and paste the following code into this file.

  1. var path=require("path");  
  2.   
  3. var srcPath=path.resolve(__dirname,"src");  
  4. var distPath=path.resolve(__dirname,"dist");  
  5.   
  6. var config={  
  7.     entry: srcPath+"/app/app.js",  
  8.     output:{  
  9.         path:srcPath+"/app",  
  10.         filename:"bundle.js",  
  11.         publicPath:"/app/"  
  12.     },  
  13.     devServer: {  
  14.       inline: true,  
  15.       port: 4500  
  16.    },  
  17.    resolve: {  
  18.     extensions: [ '.js''.jsx']  
  19.   },  
  20.     module:{  
  21.         loaders:[  
  22.             {  
  23.                 test:/\.jsx?$/,  
  24.                 exclude: /node_modules/,  
  25.                 include: /src/,  
  26.                 loader:"babel-loader",  
  27.                 query: {  
  28.                presets: ['es2015','stage-2','react']  
  29.             }  
  30.               
  31.             }  
  32.         ]  
  33.     }  
  34. }  
  35.   
  36. module.exports=config;  

This file contains the configuration settings for the webpack, here we defined the entry point and output path for the webpack. Using the “devServer” we defined the port number on which our application will run. The resolve is used to defined the file extensions for which our loaders will run. In module section we define the loader for “jsx” files and make the required configurations.

React

Our configuration file is ready for the webpack but we need to write the code that can start the webpack-server, so open “package.json” file and write following code into script section.

"start": "webpack-dev-server"

React

Step 4 Create the require files

Now create a folder in root directory of the project and name this folder as “src”. After creating the “src” folder now create another “app” and “main” folder in “src” folder. Now create an “app.js” file in “app” folder and paste the following code into this file.

App.js

  1. import React from 'react';  
  2. import ReactDOM from 'react-dom';  
  3. import Main from '../main/main.jsx';  
  4.   
  5. ReactDOM.render(<Main/>, document.getElementById('app'));  

Now create a “main.jsx” file into “main” folder and paste the following code into this file.

Main.jsx

  1. import React from 'react';  
  2.   
  3. class Main extends React.Component {  
  4.    render() {  
  5.       return (  
  6.          <h2>  
  7.             Hello React!!!  
  8.          </h2>  
  9.       );  
  10.    }  
  11. }  
  12.   
  13. export default Main;  

After creating all the required files now we need to create an “index.html” file so create a “index.html” file in root directory of the application and paste the following code into this file.

Index.html

  1. <!DOCTYPE html>  
  2. <html lang = "en">  
  3.   
  4.    <head>  
  5.       <meta charset = "UTF-8">  
  6.       <title>React App</title>  
  7.    </head>  
  8.   
  9.    <body>  
  10.       <div id = "app"></div>  
  11.       <script src = "/app/bundle.js"></script>  
  12.    </body>  
  13.   
  14. </html>  

Now the following will be the structure of our project.

React

After all these things now our project is ready to run, so open the command line terminal and run “npm run start” command.

React

When you run this command it will start the webpack-server and run the application on 4500 port. Now open the “http://localhost:4500/” URL in your browser.

React
If you are getting the above screens that means you successfully configured the React environment using the webpack and babel.

Conclusion

In this article we learned how to set up a development environment for React. In the next article we will start the work on actual concepts of React. If you have any issues or get any errors related to environment setup then write it in the comment sections.

Thanks for reading the article.


Similar Articles