user1571934 user1571934 - 3 months ago 23
Node.js Question

npm packages client side with browserify

I am trying to use the NPM package leaflet-pip for my client-side app.

In index.js

var lpip = require('leaflet-pip');


The I typed the command into my console


browserify index.js > bundle.js


In index.html

<script src="bundle.js"></script>
<script type="text/javascript">
var mymap = L.map('map');
function clickHandler(e) {
mymap.eachLayer( function(overlay) {
if(leafletPip.pointInLayer(e.latlng, overlay.feature)) {
console.log(overlay.feature);
console.log("Inside!");
}
}
}
mymap.on("click", clickHandler);
</script>


and I keep getting the error


Uncaught ReferenceError: leafletPip is not defined

Answer

You need to add leafletPip to the window object within the bundle.

so var leafletPip = window.leafletPip = require('leaflet-pip‌​') should work