- Published on
How to Fix: Can't Resolve React Router Dom (in 10 minutes)
- Authors
- Name
- Alejo G.B
- https://twitter.com/reactshark
You may have received the error: can't resolve 'react-router-dom’
Other keywords for this error are module not found:
Fixing "Can't resolve react router dom"
You have to install the dependency correctly. You may have typed “react-dom” into the console.
⚠️ And that is wrong! It is react-router-dom
, otherwise, by typing react dom
you are installing the wrong dependency. So, check on that.
Keep seeing the error after installing the correct dependency?
If you've made sure you have installed the correct dependency.
But still keep seeing that error then do:
npm install react-router-dom --save
or
npm install -S react-router-dom
Why that works?
The —-save
option creates a dev dependency on your package.json
file, which at the same time of solving this error for you, will allow you to automatically install that dependency in other computers and locations.module not found: error: can't resolve 'react-router-dom'
Update NPM to version 5
"Before version 5, NPM simply installed a package under node_modules
by default. When you were trying to install dependencies for your app/module, you would need to first install them, and then add them (along with the appropriate version number) to the dependencies
section of your package.json
." Read more here.
If you want to solve that kind of issues, like Can't resolve to react router dom or even Can't resolve axios, then update NPM to the latest version by doing:
npm update -g
Understanding the nature of this error
Can't resolve in most cases refers to an installation problem in your React app.
And it's the common case that you don't have the dependency you are trying to use.
import { Link } from 'react-router-dom'
One easy way to check if you have installed a dependency, in VS Code, is to press ctrl + left click
on the "react-router-dom"
words. If you get redirected to the origin, node_modules file of the dependency, then it's been installed, if not, correct that, install it again using the previous techniques.
Using TypeScript?
When using Typescript for a React project, you need to run the following command:
npm i @types/react-router-dom
Conclusion
Make sure to follow the first half of this article, because that’s where the central part of this error is coming from.