Search code examples
javascriptleafletmapboxrequire

Converting Geojson to Shapefile in Javascript using shp-write


I'm using Mapbox, and I'm trying to convert a Geojson to a zipped shapefile using shp-write. But when I follow the example given on the GitHub page, I'm getting a "ReferenceError: require is not defined" error on this line:

var shpwrite = require('shp-write');

This is a jsfiddle in which you can test this. I'm relatively new to JavaScript, and haven't had to use the 'require()' function before.


Solution

  • The jsfiddle you provided includes several external resources, among which a shpwrite delivered by unpkg that you may be missing.

    Usually require doesn't exist in your browser. You would need to execute it with Node.js or use a module bundler like Webpack, but Unpkg takes care of it for you.

    So adding:

    <script src="https://unpkg.com/shp-write@latest/shpwrite.js"></script>
    

    on your page should make it work.