How to resolve 'undefined Module' error when loading npm package in NodeJS app

08-23-2020 05:04 PM
Occasional Contributor II

I am trying to load a module called 'geojson2h3' in my `NodeJS` app with the arcgis api for javascript methods to render the hexagons with the h3 spatial index. After running `npm install geojson2h3` into my node_modules folder, I tried following the initial code instructions given in `Github` to test it out:

import geojson2h3 from 'geojson2h3';

const polygon = {
    type: 'Feature',
    geometry: {
        type: 'Polygon',
        coordinates: [
            [-122.47485823276713, 37.85878356045377],
            [-122.47504834087829, 37.86196795698972],
            [-122.47845104316997, 37.86010614563313],
            [-122.47485823276713, 37.85878356045377]

const hexagons = geojson2h3.featureToH3Set(polygon, 10);
// -> ['8a2830855047fff', '8a2830855077fff', '8a283085505ffff', '8a283085506ffff']

const feature = geojson2h3.h3SetToFeature(hexagons);
// -> {type: 'Feature', properties: {}, geometry: {type: 'Polygon', coordinates: [...]}}

This method throws a `SyntaxError: Cannot use import statement outside a module` error. I then tried using the `NodeJS require` method as such: `const geojson2h3 = require('geojson2h3');`, replacing the `import` statement. This throws an `Uncaught Error: undefinedModule` error. The package shows in my `package.json` filer. What did I do wrong here?

0 Kudos
0 Replies