react-router-bootstrap: Can't Resolve react-router-dom
I am using react-bootstrap and react-router in my React application. I installed react-router-bootstrap so I could use Nav.Item and LinkContainer for my routes. Originally when I set it all up I kept getting this error.
./node_modules/react-router-bootstrap/lib/LinkContainer.js Module not found: Can’t resolve ‘react-router-dom’ in ‘/Users/wuno/Dropbox/Hack/Devops/react/riverwalk-credit-web/node_modules/react-router-bootstrap/lib’
I left the computer alone for a bit came back and started up the server again, npm start and it worked fine. At this point I decided to do some refactoring which broke it again giving me the same error as mentioned above. At this point I set everything back to the last working state and the error was still there.
I checked the node_modules directory and I do not see a react-router-dom inside of the lib folder or where it is even referencing this. But when I check the repo on git there does seem to be a reference for the library inside of LinkContainer.
I am thinking this must be a bug but this is my first time using any of these tools. All my experience is with react-native.
index.js
import { Router, Route } from 'react-router';
<Router>
<div>
<Navigation />
<Route exact path="/" component={HomeContainer} />
</div>
</Router>
Navigation
import { LinkContainer } from 'react-router-bootstrap';
<LinkContainer to="/">
<NavItem>Home</NavItem>
</LinkContainer>
Error
./node_modules/react-router-bootstrap/lib/LinkContainer.js Module not found: Can’t resolve ‘react-router-dom’ in ‘/Users/wuno/Dropbox/Hack/Devops/react/riverwalk-credit-web/node_modules/react-router-bootstrap/lib’
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 2
- Comments: 34 (4 by maintainers)
Have you tried installing react-router-dom
did you install react, react-dom, react-router-dom?
If not,
npm i react react-dom react-router-dom@princecharming519 , I assume you’re using docker. If so, drop your images and recreate them. This solved my problem.
@babusng with the level of details you provided it’s impossible to help you to resolve the issue.
My suggestion is to run
npm install react-router-dom(oryarn add react-router-domif you’re using Yarn). Then check that the import is referencing package name, not path (i.e., it should berequire('react-router-dom'), notrequire('./react-router-dom'))Actually I just now got it to work. You were right that is what it needed but the import is like this,
import { BrowserRouter as Router, Route } from 'react-router-dom';Thanks for your help. Is there docs anywhere explaining this?
try this way too , it will work
@wuno glad that you managed to resolve the issue you had.
Have a look at react-router’s installation guide, which has a note about packages that should be used for development. You’d never reference
react-routerdirectly in your web application becausereact-router-domis supposed to be used instead.Did you try to restart dev server?
Wasted so much time with this 🤦♂️
I got the same error when using create-react-app with Ruby on Rails. Turned out that after initialization, all the packages are installed under
/clientfolder. I need tocd clientbefore runningyarn add react-dom react-router-domso that the server can correctly find the packages.just install react-router-dom with --save and you are good to go
Here is the code:
I got the same kind of error. Please how can I fix this as I am new to react?
In my case i used “yarn add react-router-dom” yarn and it worked perfectly but "npm i react-router-dom --save " was not working.
You’re right! I totally had it in the wrong directory! Sorry about the inconvenience and thanks for taking a look!
@lopezdp this has nothing to do with
react-router-bootstrap. Please check the structure of your project and paths of the files you’re referencing in your imports.I am dealing with this same issue on two new projects I have just started and I have it configured the way I have it on another in production and for some reason I can’t seem to find the bug here.
Here are my dependencies in my
package.json:I have an
index.jsfile where I am wrapping my<App />component inside of the<Router />component that I am importing withBrowserRouteras specified fromreact-router-dom.Here are the contents of my
index.jsfile:From there my
<App />component implements a custom<Routes />component that usesRouteandSwitchfromreact-router-domto handle my app’s routing needs.Here are the contents of my
<App />component:This is what my
Routes.jsfile looks like and how it uses theRouteandSwitchfromreact-router-dom:Here is the
{ Home }component that I am trying to serve up locally:And finally here is the error:
What do you think could be the problem here??? Thanks !
wow! thanks man. its been resolved