ReactJS is a JavaScript library for creating attractive user interfaces, with this library we can build single-page applications. React is easy to learn and create projects. In today's time, the demand for React is increasing a lot. But there are some important steps to use React that we have to follow.
Each operating system requires slightly different steps for installing React. In this article, we'll tell you everything from scratch to how to install ReactJS on a Windows PC and how to use it in our first project.
So let's get started with step-by-step instructions!
Guide Outline
Step 1: Install Node.js and npm
- Download node.js
- Install node.js
- Verify Installation
Step 2: Install Create React App
Step 3: Create Your First React Project
- Navigate to the Desired Directory
- Create a React App
- Navigate to the Project Directory
- Start the Development Server
Step 1: Install Node.js and npm
This version of Node.js runs JavaScript code outside a web browser by using Chrome's V8 JavaScript engine. One of its special features is that with Node.js, you get npm (Node Package Manager), which helps you manage and install libraries and tools for your projects.
To install React, you should first ensure that Node.js and npm are installed on your system. If they aren't already installed, follow these steps:
Download node.js
Visit the Node.js official website.
Download the installer for your Windows system. You can choose either the LTS or the Current version, but the LTS version is recommended for most users.
Install Node.js
Once downloaded, open the NodeJS installer. Keep the default settings, and click on the Next button until the installation is finished. Just make sure that npm is included in the installation.
Verify Installation
Open the Command Prompt by pressing Win + R, typing "cmd", and pressing Enter. Then, type the following command to check if the installation is complete.
// For nodejs
node -v
//You should see the Node.js version number.
// For npm
npm -v
//You should see the npm version number.
These commands will display the version numbers for Node.js and npm if the installation was successful.
Step 2: Install Create React App
Create React App is an officially supported way to create single-page React applications. It offers a modern build setup with no configuration.
To install Create React App globally, open Command Prompt by pressing Win + R, type "cmd", and press Enter. Then, run the following command and press enter.
npm install -g create-react-app
This command installs Create React App globally on your system, making it available to use in any project and you only need to do this once. After that, you can create new React projects without fetching them again.
Step 3: Create Your First React Project
Navigate to the Desired Directory
Use the cd (change directory) command to navigate to the folder where you want to create your React project. If you haven't created a folder yet, create one wherever you prefer and navigate to that folder using the terminal or Git Bash.
For example:
cd path\to\your\desired\directory
Create a React App
Run the following command to create a new React project:
npx create-react-app my-first-react-app
You can replace my-first-react-app with your desired project name. This command will create a new folder named my-first-react-app with all the necessary files and dependencies. Kindly be patient because this command takes some time to set up the project.
If you want to install all necessary files and dependencies without creating a new folder inside an existing folder, use the command "npx create-react-app ." instead of specifying a folder name. This command will set up the React project in the current directory.
Important: If you have already installed Create React App globally using npm install -g create-react-app, you can simply use npx create-react-app my-first-react-app to create a new project. However, using npx create-react-app my-first-react-app ensures you always use the latest version of Create React App.
Navigate to the Project Directory
Once the project is created, change into the project directory using the cd command:
cd my-first-react-app
Start the Development Server
To start the React application, run the following command:
npm start
This will launch the development server, and your default web browser will automatically open the new React application at localhost:3000.
I hope you've successfully installed React on your Windows PC and created your first React project.
Conclusion
When installing React.js for your first project, begin by installing it globally with the command 'npm install -g create-react-app'. Afterward, there's no need to install React globally again for future projects. Simply create a folder for your new project, navigate to it in the terminal using 'cd', and then run 'npx create-react-app my-first-react-app'. This command will set up your new React project.
This approach eliminates the need to reinstall React globally each time you begin a new project.
If you have any questions about this topic or related to web development, you can ask them in the question box given below, and you will get the answer soon.