Search code examples
javascriptreactjsreact-routercommonjs

react-router history using CommonJS


The React-Router documentation shows that for including browser history you include it like such:

import { browserHistory } from 'react-router'
. 
. 
.     
<Router history={browserHistory} />

How does one do this with CommonJS?

When I try it using the import method I get Illegal import declaration while parsing file


Solution

  • The import syntax isn't implemented in browsers yet (hence the error you're getting, I'd imagine), so you need a transpiler like Babel to use it. The equivalent CommonJS syntax is:

    var browserHistory = require("react-router").browserHistory
    

    It mentions this on the GitHub page (although it doesn't specifically mention browserHistory).