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.

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

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
IvanSanchezcommented, Dec 2, 2015

@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.

1reaction
stefan--commented, Jun 8, 2017

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js Leaflet error - Stack Overflow
It's an issue with Leaflet itself. Leaflet tries to load the DOM without checking if it's available or not. I think you can...
Read more >
require is not defined babel | The AI Search Engine You Control
You're trying to use a CommonJS module from within your browser. This will not work. How are you using them? When you write...
Read more >
Leaflet with browserify - Sigon - GitLab
This post will will show you how to build a very simple application that uses leaflet js with browserify. browserify logo ...
Read more >
leaflet-src.js in extensions/piwigo-openstreetmap/leaflet
21, // define Leaflet as a global L variable, saving the original L to restore ... 514, // terrible browser detection to work...
Read more >
leaflet - UNPKG
The best place to put your Leaflet plugin to is a separate [GitHub](http://github.com) repository. 27, If you create a collection ...
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