Reputation: 85
I tried to run leaflet on a nodejs server side without success. I build it with jake as described in the download section, but then, when I require leaflet on a server file, if I start my node server, it crash with this error :
ReferenceError: window is not defined
Thanks node, I know it. But Is there a way to use leaflet on server side ? I need it for some operation on a L.geojson (https://github.com/mapbox/leaflet-pip) and I can't manage to do it without the "L" reference.
I'll appreciate any help. Thanks.
Upvotes: 8
Views: 8017
Reputation: 5353
You can load leaflet in node.js by simulating the browser:
// Create globals so leaflet can load
global.window = {
screen: {
devicePixelRatio: 1
}
};
global.document = {
documentElement: {
style: {}
},
getElementsByTagName: function() { return []; },
createElement: function() { return {}; }
};
global.navigator = {
userAgent: 'nodejs',
platform: 'nodejs'
};
global.L = require('leaflet');
I used this in conjunction with Point in Polygon for Leaflet.
Upvotes: 14
Reputation: 4229
Vanilla Leaflet does not work in node. I've made a wrapper here: https://github.com/jieter/leaflet-headless
Upvotes: 2