23

I'm learning from this tutorial but I keep getting this error:

'react-router' does not contain an export named 'browserHistory'.

The file that has react-router is this:

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, browserHistory } from 'react-router';
import routes from './routes';

ReactDOM.render(
  <Router history={browserHistory} routes={routes} />, 
  document.getElementById('root')
);
Preview
  • 35,317
  • 10
  • 92
  • 112
stackjlei
  • 9,485
  • 18
  • 65
  • 113
  • you can refer to this link as it already has an answer here http://stackoverflow.com/questions/35063095/react-router-browserhistory-not-working-as-expected – Vikram Saini May 19 '17 at 06:52
  • 1
    Did you check github? https://github.com/ReactTraining/react-router/issues/4732 – StudioTime May 19 '17 at 06:56
  • 1
    I think, you are using the latest version of React-router which doesn't contain the export browserHistory, rather than downgrading to a lower version, its better to change your code to work with the latest version Check these docs: https://reacttraining.com/react-router/core/guides/quick-start – Shubham Khatri May 19 '17 at 07:09

6 Answers6

35

You need to get browserHistory from the history module now.

import createHistory from 'history/createBrowserHistory'

Note that they changed the module API recently so if you are using the latest version the import slightly changed:

import { createBrowserHistory } from 'history'
Preview
  • 35,317
  • 10
  • 92
  • 112
8

I had the same problem and I wasted a couple of days to figure it out. This error happens simply because react-router v4 does not have the browserHistory (I don't know if that's a good thing or not though). I solved the issue by installing v3 like this:

npm install react-router@3 --save
Karol Selak
  • 4,248
  • 6
  • 35
  • 65
Kv B
  • 97
  • 1
  • 1
4

You are using version 4 of react-router.

Either downgrade the package or follow the instructions in this SO answer to make it work with v4.

spazm
  • 4,399
  • 31
  • 30
DalSoft
  • 10,673
  • 3
  • 42
  • 55
1

Simple Solution

method 1:

npm install --save history

use this now:

import createHistory from 'history/createBrowserHistory'

method:2

Use Version 3  

npm install react-router@3
ravibagul91
  • 20,072
  • 5
  • 36
  • 59
Shashwat Gupta
  • 5,071
  • 41
  • 33
0
import browserHistory from "history/createBrowserHistory";

use

const routermiddleware = routerMiddleware(browserHistory());
const history = syncHistoryWithStore(browserHistory(), store);
Rahul Shakya
  • 1,269
  • 15
  • 15
0

In react-router-dom version 6 useHistory() is replaced by useNavigate() ;

import {useNavigate} from 'react-router-dom';
const navigate = useNavigate();
navigate('/home')
   

If react-router-dom version less than 6

import { useHistory } from "react-router-dom";
const history = useHistory();
history.push('/home')
mm_
  • 1,566
  • 2
  • 18
  • 37
Gunjan Kumar
  • 127
  • 1
  • 5