Module not found: Can't resolve 'react-router-dom'
Problem
I got the following error when I tried to import and use the 'react-router-dom' package in my React application.
Module not found: Can't resolve 'react-router-dom' in 'D:\Projects\MyProject\src'
My code is given below.
import {Route} from 'react-router-dom';
import Page1 from './pages/Page1';
import Page2 from './pages/Page2';
import Page3 from './pages/Page3';
function App() {
return <div>
<Route path='/' exact>
<Page1/>
</Route>
<Route path='/page2'>
<Page2/>
</Route>
<Route path='/Page3'>
<Page3/>
</Route>
</div>;
}
export default App;
Solution
I didn't install the react-router-dom package in my react application. That was the reason for the error. I installed the package using the following command.
npm install react-router-dom
This fixed the issue. This package allows us to add routing functionality to React.