Installing React Router
In this lesson, we will install React Router, understand the installation command, and prepare a React project for routing.
1. Why Do We Install React Router?
React does not include a complete routing solution by default. React Router is a separate library that provides routing components and hooks for navigating between views.
Before using components such as BrowserRouter, Routes, Route, and Link, we need to install the appropriate React Router package in our project.
Remember: Install React Router in the React project where you want to use its routing features.
2. Prerequisites
Before installing React Router, make sure you have:
- Node.js installed on your computer.
- npm available in your terminal.
- A React project created on your system.
- A code editor such as Visual Studio Code.
Check Node.js
node -v
Check npm
npm -v
These commands display the installed versions of Node.js and npm.
3. Create a React Project
If you do not already have a React project, you can create one using Vite.
npm create vite@latest react-routing-demo -- --template react
Move into the newly created project directory.
cd react-routing-demo
Install the project's existing dependencies.
npm install
If you already have a React project, skip the project creation command and open your existing project folder.
4. Install React Router
Open a terminal in your React project directory and run the following command for the declarative routing examples in this module.
npm install react-router-dom
npm downloads the package and adds it to the project's dependencies.
Check package.json
After installation, you can check the dependencies section of your package.json file.
{
"dependencies": {
"react": "...",
"react-dom": "...",
"react-router-dom": "..."
}
}
The dots represent version values managed by npm; they are placeholders in this example, not literal package versions.
5. Understand the Installation Workflow
Once installation is complete, the application can import React Router components and configure routes.
6. Import React Router Components
After installation, you can import the components needed for routing from react-router-dom.
import {
BrowserRouter,
Routes,
Route,
Link
} from 'react-router-dom';
These imports make the selected routing components available to your React code.
- BrowserRouter: Enables browser-based routing.
- Routes: Groups route definitions.
- Route: Defines a URL path and its element.
- Link: Provides client-side navigation.
7. Test the React Project
Start the development server from your project directory.
npm run dev
Vite displays a local development URL in the terminal. Open that URL in your browser to view the React application.
Expected Result
- The development server starts successfully.
- The browser displays your React application.
- The project is ready for routing configuration.
Important: Installing React Router alone does not automatically configure your routes. You must set up the router and define the required paths.
8. Common Installation Problems
Problem 1: npm is not recognized
Node.js may not be installed correctly, or its installation directory may not be available in the system PATH. Install Node.js and reopen the terminal.
Problem 2: Wrong project directory
Make sure the terminal is open in the React project directory containing package.json before running the installation command.
Problem 3: Package installation fails
Check your internet connection, npm error message, and project dependency compatibility before trying again.
Problem 4: Import error
Verify that the package is installed in the correct project and that the import statement uses the correct package name.
Practice Tasks
- Check the installed Node.js and npm versions.
- Create a new React project using Vite.
- Install the project dependencies using npm install.
- Install React Router using npm install react-router-dom.
- Locate react-router-dom in package.json.
- Import BrowserRouter, Routes, Route, and Link.
- Start the development server using npm run dev.
- Write down any installation error and explain how you resolved it.
Summary
React Router is installed as a package in a React project using npm install react-router-dom. Before installation, ensure that Node.js, npm, and a React project are available. After installing the package, import the required routing components and start the development server to test the application. In the next lesson, we will learn how to configure BrowserRouter and set up routing in a React application.