Leaflet: Leaflet "window is not defined" when working with Browserify due to use of "this"

I’m receiving the error “window not defined” which stems from leaflet’s use of this when referring to window. I have 0.7.3 of the npm leaflet version.

window is not defined
    at Object.<anonymous> (/node_modules/leaflet/dist/leaflet-src.js:9180:3)

This issue references it but the issue does not seem to actually be resolved despite the closed status.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 17 (8 by maintainers)

Most upvoted comments

@dougajmcdonald @vincentaudebert This is what the react-starter-kit folks have to say about that:

https://github.com/webpack/react-starter/issues/37#issuecomment-93943419

Don’t use libraries that depend on window in your components. or Only use these libraries in componentDidMount and exclude them for prerendering (make sure your component don’t render different in prerendering or it doesn’t work).

In other words, I’m making this not our (Leaflet’s) problem. If this is not reproducible with pure Browserify and no react, I’ll close the issue.

This issue might still arise in certain circumstances, e.g. protractor E2E testing.

Given you use ES6 or TS and import Leaflet as a module with import * as Leaflet from "leaflet" in a given class, when instantiating this class before the DOM is loaded you will receive ReferenceError: window is not defined.

The reason for instantiating this class depending on Leaflet before the DOM is ready could be to build objects for a test scenario. @mourner I believe this is worth considering in further iterations.