gwydion93
gwydion93

Reputation: 1923

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

I am trying to load a package called 'geojson2h3' in my NodeJS app. After running npm install geojson2h3, 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: [...]}}

Of course, this 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 file, but I don't see it in my node_modules folder. I am assuming that's related to the issue. What did I do wrong here?

*Note: I know that this is a GIS-related package but the issue is a generic NodeJS package issue, not a GIS-specific one.

Upvotes: 0

Views: 1663

Answers (2)

Cisum Inas
Cisum Inas

Reputation: 12990

This is related to how the project is build, basically it does not export a solid version for nodejs. strong text

So to summarise the problem, the peerdependency of h3-js, does not seem to be es-module friendly as you can se below. enter image description here

As far as I can tell, the easiest solution for you is to try too use something else open source, but if there is nothing that meets your expectation, I guess you could try to modify h3-js. from node_modules/h3-js.js and the push for necessary changes on the relevant github pages. enter image description here

Upvotes: 1

domenikk
domenikk

Reputation: 1743

Try: npm i to install that package.

Upvotes: 0

Related Questions