. Advertisement .
..3..
. Advertisement .
..4..
“Failed to compile ./src/components/App/App.js Module not found: Can’t resolve ‘react-router-dom’” is a common error that many programmers get. It happens in may ways. What is the cause of this error and how to fix it? Let’s read the article below to find the best answer.
When Does The Error “Failed to compile ./src/components/App/App.js Module not found: Can’t resolve ‘react-router-dom'” Happen?
The browser gives the following warning shortly after npm launches:
Failed to compile ./src/components/App/App.js Module not found: Can't resolve 'react-
router-dom'.
What Is ‘react-router-dom’?
An npm package which is called React Router DOM makes it possible to integrate dynamic routing into web applications. You can use it to show pages and let visitors navigate them. It is a client- and server-side routing library for React with all the features. Building single-page applications, or programs with numerous pages or components but no page refresh since the content is dynamically downloaded from the URL, requires the use of React Router Dom. React Router Dom makes it possible for this procedure, which is known as routing.
React-main router’s benefit is that it avoids the need for page refreshes, for instance, a link to another page is clicked. In addition, comparing to conventional page navigation, it is quite quick. This indicates that improved user experience and overall better performance for the app.
How To Solve The Error “Failed to compile ./src/components/App/App.js Module not found: Can’t resolve ‘react-router-dom'”
Solution 1: Set up react-dom
The simplest solution to fix the error “Failed to compile ./src/components/App/App.js Module not found: Can’t resolve ‘react-router-dom'” is to set up react-dom. You simply need to execute both commands in your terminal. Let’s run the following command first:
npm i react-router-dom
Run this command after AND:
npm i @types/react-router-dom
Solution 2: Remove your node_modules and package-lock.json (not package.json) files
Another solution for you to solve your problem is attempting to remove your node_modules and package-lock.json (not package.json) files, then run npm install once more time and reslaunch your IDE. Look at the following program to further understand about this method:
# remove node_modules and package-lock.json
rm -rf node_modules
rm -f package-lock.json
# clean npm cache
npm cache clean --force
npm install
After doing that, your error will be completely resolved.
Solution 3: Run the following command
Install react-router-dom is all that is necessary. Simply copy the command below and run it from your terminal.
npm install react-router-dom --save
Solution 4: Open your package.json file and verify
Open your package.json file and verify that the dependencies field includes the react-router-dom package.
{
// … rest
"dependencies": {
"react-router-dom": "^6.3.0",
},
"devDependencies": {
// only when TypeScript is used
"@types/react-router-dom": "^5.3.3"
}
}
Conclusion
You may apply our methods when you encounter “Failed to compile ./src/components/App/App.js Module not found: Can’t resolve ‘react-router-dom’” error. These methods may seem complicated, but they will best solve your issue. You may use the dir(), vars() function, or the pprint module to solve the issue. Please fill in the blanks with questions and suggested responses to help us better assist you. Thank you!
Read more
→ Tips On Fixing The Error “Attempted import error: ‘Switch’ is not exported from ‘react-router-dom’”
Leave a comment