×

React Environment Setup Step by Step

React Environment Setup

This section will provide you the information about how to set up an environment for the successful development of ReactJS application:

Pre-Requisite for ReactJS

  1. NodeJS and NPM
  2. React and React DOM
  3. Webpack
  4. Babel

There are two ways of installing the ReactJS:

  1. Using npm command
  2. By using create-react-app

By using npm command

Step 1: Install NodeJS. Go to https://nodejs.org

Step 2: Click on downloads.

Download NodeJs

Step 3: Install This Node.js

Step 4: Check the version by using the following command.

Check the version by using commands

Step 5: Now, create a root folder in your desired directory with your desired name.

create a root folder in your desired directory

Step 6: Create a package.json file. This file is required for generating a module. Create this file by using the command:

npm init –y

file is required for generating a module

Step 7: Install React and its DOM Packages by using the npm command:

npm install react react-dom --save  

Install React and its DOM Packages

Step 8: Install Webpack

Webpack is mainly used for module packaging, development, and production pipeline automation.

For installing Webpack, we have to use the following command:

npm install webpack webpack-dev-server webpack-cli --save  

In the above command, webpack-dev-server is used during development, webpack to create production builds, and webpack-cli provides a set of commands.

 Install Webpack

Step 9: Install Babel.

Babel is simply a JavaScript compiler that is used for creating one source code to others. For installing Babel, use the following command:

npm install babel-core babel-loader babel-preset-env babel-preset-react babel-webpack-plugin --save-dev  

Install Babel.

Step 9: Create files. To complete the installation process, we need to add some files to the project folder. The files are:

  • index.html
  •  App.js
  • main.js
  • webpack.config.js
  • .babelrc

We can manually create these files, or by using the command prompt. For creating files, use the command prompt, and type the following command:

type nul > filename

Step 10: Now, Set Compiler, loader, and server for React Application

  1. Configure Webpack

Add the code given below in the webpack.config.json to configure the Webpack. It sets the development server to 8080 port. It is responsible for defining the loaders for processing the file types that uses within your app and wrap up by adding plugins needed during our development.

webpack.config.json constpath=require('path'); constHtmlWebpackPlugin=require('html-webpack-plugin'); module.exports={ entry:'./main.js', output:{ path:path.join(__dirname,'/bundle'), filename:'index_bundle.js' }, devServer:{ inline:true, port:8080 }, module:{ rules:[ { test:/\.jsx?$/, exclude:/node_modules/, use:{ loader:"babel-loader", } } ] }, plugins:[ newHtmlWebpackPlugin({ template:'./index.html' }) ] }
Configure Webpack

Now, open package.json file and delete “test" "echo \" Error: no test specified\" && exit 1", which is inside “scripts” object, and add the commands start and build.

open package.json file

Delete which is shown in above image and add the following two:

  1.     "start": "webpack-dev-server --mode development --open --hot",  
  2.     "build": "webpack --mode production"  

It is described in the following image:

build webpack --mode production
  • HTML webpack template for index.html

We have to add a custom template to generate index.html using the HtmlWeb-packPlugin plugin. It enables us to a viewport tag for supporting mobile scaling of our app.

Add the following code to your index.html file.

index.html ReactApp
HTML webpack template for index.html
  • App.jsx and main.js

This is the app entry point i.e., the first react component. It will render Hello World.

Add the following code to your App.js file.

App.js importReact,{Component}from'react'; classAppextendsComponent{ render(){ return(

HelloWorld

); } } exportdefaultApp;
App.jsx and main.js

Now, we have to import this component and render it to the root App element so that we can see it in the browser.

main.js

importReactfrom'react'; importReactDOMfrom'react-dom'; importAppfrom'./App.js'; ReactDOM.render(,document.getElementById('app'));
main.js
  • Create .babelrc file

Add the following code to your .babelrc file.

.babelrc { "presets":[ "@babel/preset-env","@babel/preset-react"] }

Now, run the server. For running the server, apply the following command in your command prompt within the same folder in which all react files exist.

npm start

This command will show the port number, which requires an opening in the browser. After opening it, you will get the following output:

npm start

By using the create-react-app command

Step 1: Install NodeJS. Go to https://nodejs.org

Step 2: Click on downloads.

Click on download node js

Step 3: Install This Node.js

Step 4: Check the version by using the following command.

Check the version

Step 5: Now Install NPM in your selected drive, by using the command: npm install –g create-react-app

Now Install NPM in your selected drive

Step 6:  Now create your own React project by using the command:

create-react-app  your project name

It will take some time.

create your own React project1
create your own React project1

Step 7:  Now move to your project folder by using: cd ‘your folder name.'

move to your project folder

Step 8:  Now write the command npm start

write the command npm start

Then a webpage on your browser will be opened. This is the output of your React App.

webpage on your browser will be opened

If you want to change the above output, open the App.js file, which is located in the src folder (src>>App.js), and make the changes that you want to see on the screen. Once you save the file, the webpack will recompile the code, and the page will automatically refresh, then the changes will be reflected on the screen.

For example:

The following image shows you the output after a change in the App.js file.

shows you the output after a change

Now, open your project in the editor. I have Visual studio. You can see the project’s default structure in the following image:

 open your project in the editor

There are various files and folders in the root directory of the React Application. Some are defined as follows:

1. node_modules: It includes React libraries and any other third party libraries if needed.

2. public: It contains the public assets of the application. It contains the file name index.html where React will mount the application by default on the <div id=”root”></div> element.

3. src: It contains several files like App.css, App.js, App.test.js, index.css, index.js, and serviceWorker.js files. The App.js file will always be responsible for displaying the React output screen.

4. package-lock.json: It automatically generates for any operations where the npm package modifies either the package.json or the node_modules tree. It can't be published. If it finds any other place rather than the top-level package, then it will be ignored.

5. package.json: It contains various metadata for the project. It provides information to the npm that allows identifying the project dependencies as well as the project.

6. README.md: It includes the React topics documentation.


Related Topics

ReactJS Versions

ReactJS Versions The Complete set of release history of ReactJS is elaborated as follows. The complete set of full documentation of recent releases is on GitHub. S.noVersionRelease DateExplanation1.0.3.029/05/2013Initially released for public.2.0.4.029/07/2013Supporting comments...

4 minutes read.

React Keys

React Keys A key can be defined as a unique identifier. In React, it is used for identifying the items that have changed, deleted, and updated from the lists. React Keys are helpful...

3 minutes read.

React Environment Setup Step by Step

React Environment Setup This section will provide you the information about how to set up an environment for the successful development of ReactJS application: Pre-Requisite for ReactJS NodeJS and NPMReact and React DOMWebpackBabel There are two...

5 minutes read.

React Router

What is React Router? Routing is a process by which a user can direct to different pages based on their actions and requests. ReactJS router is generally used to develop single...

10 minutes read.

React Constructors

What is constructor? The constructor is a method that is used for initializing the state of the object in the class. It calls automatically during the creation of an object in...

4 minutes read.

React Conditional Rendering

React Conditional Rendering In React, the working of the conditional rendering is similar to the condition works in JavaScript. We use JavaScript operators for creating elements that represent the current state, and then the React...

3 minutes read.

Difference between Controlled and Uncontrolled component in ReactJs

Controlled Component In the controlled component, the input of the form element is handled by the component rather than the DOM. Controlled components have the functions that govern the data, which...

2 minutes read.

React Props

React Props A prop is an abbreviation of ‘properties.' State and props are mainly different from each other because props are immutable. Props are the read-only components. It is the object that stores the attribute...

3 minutes read.

React Events

React Events An event is an action that triggers a response of the user action or system-generated event. As HTML, React can also perform actions based on user events. React has the...

2 minutes read.

React Code Splitting

React Code Splitting React Code Splitting is the procedure of splitting the bundle files by which the files get easily loaded on the webpage. The react application bundles the files by using...

4 minutes read.

React Redux

How to Use Redux with React Hooks It is an open-source library of JavaScript which is used for managing the application state. React uses the Redux to build the user interface....

5 minutes read.

Comparison between React and Vue

Comparison between React and Vue React, and Vue both are the two most famous libraries of JavaScript that are used for creating thousands of websites today. The React and Vue both...

3 minutes read.

React Components

React Components Former, the developers used to write more than thousands of lines of code to develop a single-page application. These applications follow the structure of traditional DOM, and it was very challenging to make...

2 minutes read.

React Hooks

React Hooks The Hooks were introduced in React 16.8. By using React Hooks, you can use the state and other features of the React without writing a class. Hooks do not work within the classes....

5 minutes read.

Difference Between React Flux and MVC

React Flux vs MVC MVC MVC is an acronym of 'Model View Controller.' It is the architectural pattern that is used to develop the user interface. It has three different logical components: The...

2 minutes read.

React Fragments

React Fragments The fragments in React are introduced from the 16.2 and above versions. Fragments allow you to group a list of child elements without adding any extra node in the DOM. In React,...

2 minutes read.

React Lists

React Lists Lists are used for displaying the data in an ordered format and mainly used to display the menus on websites. The creating of lists in React is as similar...

2 minutes read.

React Error Boundaries

React Error Boundaries In the past, the errors of JavaScript within the components were used to corrupt the internal state of react and cause it to emit the cryptic errors on the next...

3 minutes read.

React Props Validation

React Props Validation As we know, props are a suitable mechanism for passing the read-only attributes to react components. The props are required to be correctly used in the component. If it is...

5 minutes read.

React Table

React Table The table represents an arrangement that organizes the information in the form of rows and columns. The table is mainly used for storing and displaying the data within a structured format. Features of...

2 minutes read.