question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invariant Violation: Browser history needs a DOM on `npm run build`

See original GitHub issue

React 16, Node 9.4.0, Mac OSX

I’m migrating from CRA to react-static.

I’ve setted up react-static-routes and importing everything from react-static instead of react-router-dom for sure.

[BABEL] Note: The code generator has deoptimised the styling of "/Users/justfly/projects/taxi-static/dist/static.266c1799.js" as it exceeds the max of "500KB".
{ Invariant Violation: Browser history needs a DOM
    at invariant (/Users/justfly/projects/taxi-static/node_modules/invariant/invariant.js:42:15)
    at createBrowserHistory (/Users/justfly/projects/taxi-static/node_modules/history/createBrowserHistory.js:49:27)
    at Object.defineProperty.value (/Users/justfly/projects/taxi-static/dist/static.266c1799.js:4453:15)
    at __webpack_require__ (/Users/justfly/projects/taxi-static/dist/static.266c1799.js:30:30)
    at Object.defineProperty.value (/Users/justfly/projects/taxi-static/dist/static.266c1799.js:8741:16)
    at __webpack_require__ (/Users/justfly/projects/taxi-static/dist/static.266c1799.js:30:30)
    at Object.<anonymous> (/Users/justfly/projects/taxi-static/dist/static.266c1799.js:8496:16)
    at __webpack_require__ (/Users/justfly/projects/taxi-static/dist/static.266c1799.js:30:30)
    at Object.defineProperty.value (/Users/justfly/projects/taxi-static/dist/static.266c1799.js:73:18)
    at /Users/justfly/projects/taxi-static/dist/static.266c1799.js:11:17 name: 'Invariant Violation', framesToPop: 1 }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

37reactions
JustFly1984commented, Feb 3, 2018

I solved an issue.

The reason was that I had two instances of Router in my app - one for main app, and second for modal window. I could not use BrowserRoutes, cos both instances required the same history object created by createBrowserHistory, and in my case it was instantiated on server side, which leads to an error.

Solution is simple refactor to history instantiation:

let history

if (typeof document !== 'undefined') {
  const createBrowserHistory = require('history/createBrowserHistory').default

  history = createBrowserHistory()
}

export default history
6reactions
joaopedromattoscommented, Dec 8, 2018

@JustFly1984 Hi, I am learning react, I have the same problem. My question is: what file I can write this code? Thanks. 😃

let history if (typeof document !== ‘undefined’) { const createBrowserHistory = require(‘history/createBrowserHistory’).default history = createBrowserHistory() } export default history

I want to know that too! I have the same issue and I don’t have any clue about in what file should I write this code.

EDIT

Solved this issue by removing the <BrowserRouter> tag from my App.jsx file.

Before:

 return (
    <BrowserRouter>
      <Switch>
        <Route path="/" component={LandingPage} />
        <Redirect to="/" />
      </Switch>
    </BrowserRouter>
  );

After

return (
      <Switch>
        <Route path="/" component={LandingPage} />
        <Redirect to="/" />
      </Switch>
  );
Read more comments on GitHub >

github_iconTop Results From Across the Web

react-router 4 - Browser history needs a DOM - Stack Overflow
However when I run the code I am getting Invariant Violation: Browser history needs a DOM in the browser. my server.js file code...
Read more >
invariant violation browser history needs a dom react-router-dom
Server side rendeing using React Router: invariant violation browser history needs a dom react-router-dom : r/reactjs.
Read more >
React Js: Invariant Failed: Browser History Needs A Dom
Invariant Violation : Browser history needs a DOM on npm run build #343 and importing everything from reactstatic instead of reactrouterdom for sure...
Read more >
Tag: React Router - somewhat abstract
Error: Invariant failed: Browser history needs a DOM. Error: Invariant failed: Browser ... import {BrowserRouter, StaticRouter} from "react-router-dom";.
Read more >
Server-side rendering with React Router "Browser history ...
Coding example for the question Server-side rendering with React Router "Browser history needs a DOM"-Reactjs.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found