You can’t resolve ‘react-router-dom’ error code? Keep reading!
The libraries available in the React Coding Platform have their different specifications, in regard to what source code you wish to create.
And we know better than anyone, that not using the correct library will not create the application to your liking.
Here’s how to fix it:
A way to fix the “React-Router-Dom” library issues is to make sure you have all dependencies installed and set up.
Moreover, do a full inspection of the data files of the React platform, repair them and update the NPM of React to version 5.
Let’s next unwrap all about the libraries of React and learn about their functionality.
More About ‘Module Not Found: Error: Can’t Resolve ‘React-Router-Dom’…
When you become a React developer, you’ll gain access to a total of three libraries that you’ll be able to use for your projects.
One of the libraries you could access (primarily used for coding web applications) is the “React-Router-Dom” library.
Here’s a list of all available React libraries and what they include:
✅ React-Router
The most basic library, including all custom components and features from the rest of the libraries.
✅ React-Router-Dom
Includes components for specialized web applications development (Facebook, Instagram, Twitter)
✅ React-Router-Native
Used for building React Native Framework that helps the development of mobile applications and platforms. The issue you’re getting, on the other hand, is whenever your access to the given library has been denied.
This problem is likely to occur when particular dependencies of the library are missing or there’s an issue in the data files of your React application.
Let’s move on to the guide and learn how to address this problem!
How to Fix “Can’t Resolve React-Router-Dom”?
There are multiple ways to address the problem that occurred with your React web library.
In the upcoming guide, you’ll learn how to set up your dependencies properly, get the NPM update and scan the data files of React, to validate an error with the files.
Let’s dig deeper!
Solution #1 Set Up the Dependencies Properly
Misspelling a dependency’s name will prevent your code from accessing the dom library, used for your development.
Users tend to often spell the library’s name incorrectly, due to common typing errors or not knowing the full and complete name of the dependency.
Typing react-dom or react-dom will not trigger the dependency’s installation and the library will remain closed.
What we’re trying to say, is that in coding, even the slight mistake of a symbol or letter could result in the whole application giving an error,
How to Install the Dependencies Appropriately?
Here’s the command you should do for acquiring the dependencies:
- ‘react-router-dom’ and then press the ‘Enter’ key
If that didn’t work, try any of the following dependency codes:
Either of these commands should get the library’s dependency added.
Note: Try to run your code after correcting dependency and watch out for the error.
Solution #2 Update the NPM’s Version to 5
Before version 5, the NPM installed a package by default, tinder the node_modules section.
If you’re attempting to install dependencies for your application/mobile, you’ll have to install it first and then add it to the ‘package.json’ dependencies list, manually.
By updating to version 5, that’ll be done automatically for you, which may target the error with your React’s dom library.
Here’s how to update the NPM’s version to ‘5’:
- Clear out your current code (or add a new sheet).
- Tap on the search field.
- Do the following command: ‘npm update -g’
- Press the “Enter” key.
- Wait for the installation of the new NPM to conclude.
- Test…
- Do the same command, on your problematic sheet of code.
Only after trying the command on an empty list, it’s best to apply the NPM update to your coding sheet.
It’ll manage the dependencies that exist in the package.json and make things easier for you, by saving you some extra manual work.
Solution #3 Download Missing Dependencies
Not having particular dependencies, or downloading incorrect ones, could prevent your code from launching appropriately.
In this solution, we’ll teach you how to check whether you have the correct set of dependencies installed and how to get any missing components.
Follow these steps to validate your React dependency list:
- Locate the “react-router-dom” word in your code.
- Press and hold the ‘Ctrl’ key.
- Left-click on the “react-router-dom” word while holding the Ctrl.
- Wait to be redirected to the “node_modules” file of the dependency.
Once in the node_modules list, do the following commands to install the components:
You’ll be alerted if the node_modules list got updated in regard to components, after executing either of the commands above.
If you do notice that changes have been done to the list, try to run your code and check if the issue is fixed.
Note: Try each command at a different time. Do not type them both at once.
Solution #4 Render the ‘Route’ Component
Based on what many users had to say about this problem, we’ll assume that you’re getting this error due to the ‘Route’ component, not being rendered into a ‘Routes’ component.
The ‘Routes’ component is what handles the route path, required for ‘react-router-dom@6’.
Here is an example of a working ‘Route/s’ component usage:
Take hints from the following sheets of code and make sure you’re rendering the ‘Route’ component correctly, so you could access the ‘dom’ library in react.
Note: Receiving a different kind of error when trying to run the code is also possible.
Solution #5 Validate Common Code Mistakes
Common Code Mistakes is an error that any programmer, regardless of their skill level, could have slipped by in their projects.
Missing a dot, or a comma or simply making an error point to the left, instead of the right, could result in numerous errors when running the code.
On top of that, such small-time mistakes are really difficult to detect, especially if your code is longer.
What you should do is go through every single line of code you’ve done in React and make sure all typing rules are met.
Typing mistakes could be made when inserting the following symbols:
- “;”
- “</>”
- “=, -, +”
- “.”
Even making a dot on the inappropriate line could ruin the code for you and make it extremely difficult for you to spot.
Note: React tends to have a “problem identifier” that’ll help you detect typing errors.
Solution #6 Delete Node Modules & “Package-Lock.json”
If nothing worked so far, we strongly suggest reinstalling your node modules and getting rid of the json package-lock.
If there’s an issue with the dependencies themselves, there is no better way to address the problem than reinstalling them.
Important: Execute the commands you’re about to see in the chronological order you see them.
Otherwise, the React platform may give you an error.
Here are all the commands you’ll have to do:
After executing all of the following commands and the ‘npm install’ afterward, your dependencies will all be reset and ready to go.
Check if you would still get issues with the ‘react-router-dom’ command anymore.
Alert: The commands in this solution must be done in chronological order for them to work.
Quick Recap:
We’ve learned how to deal with the can’t resolve ‘react-router-dom’ error message.
Simply install the correct dependencies, update the NPM to version 5, and force a reinstall of the JSON file and the node modules. Validate common code mistakes.
We hope that this guide was beneficial and for more relevant content, check our blog!
Read Next: Does a Router Store Information? – All You Need to Know
Kevin has over five years of experience working in various Tech startups and providing Technical solutions. He has contributed to many Tech publications and websites. Check out more about him here.