2

I'm trying to use h3-js inside a web worker. The error I get is:

Uncaught ReferenceError: document is not defined
    at libh3-browser.js:6
    at Module../node_modules/h3-js/dist/browser/h3-js.es.js (libh3-browser.js:2)
    at __webpack_require__ (bootstrap:19)
    at Module../src/utils/worker/methods/clusterPins.ts (clusterPins.ts:1)
    at __webpack_require__ (bootstrap:19)
    at Module../src/utils/worker/methods/aggregateAndClusterPins.ts (aggregateAndClusterPins.ts:1)
    at __webpack_require__ (bootstrap:19)
    at Module../src/utils/worker/methods/index.ts (index.ts:1)
    at __webpack_require__ (bootstrap:19)
    at Module../node_modules/babel-loader/lib/index.js?!./node_modules/eslint-loader/dist/cjs.js?!./src/utils/worker/index.ts (index.ts:1)

The only issue I was able to find is on the h3-js github related to a React-native issue, which is not related to mine.

The code looks like this:

import h3 from 'h3-js';
const h3Index = h3.geoToH3(37.3615593, -122.0553238, 7);

Interestingly I'm able to run it inside a node instance with any problems, which makes me unsure if there's something I'm missing.

Len Art
  • 23
  • 4
  • I don't have time for a complete answer, you you might wanna follow similar approach to the one in [this answer of mine](https://stackoverflow.com/a/33944817/607407), but do it for h3js. – Tomáš Zato Apr 02 '20 at 13:06

1 Answers1

1

This is a limitation of the current Emscripten build - we don't actually need document, but there are references to it deep in the Emscripten runtime code. Hopefully we'll be able to remove this in the future. In the meantime, shimming document = {} onto global or this ought to provide a workaround.

nrabinowitz
  • 55,314
  • 10
  • 149
  • 165
  • 1
    Hm, thanks for this. I've already been trying something like that but am not sure how to do this with webpack and worker loader. Anyhwo, I'm sure I'll manage. Thanks! Hopefully this won't be necessary for long. – Len Art Apr 03 '20 at 05:24
  • if you were able to fix this, can you provide an example – akashmohan Dec 08 '21 at 06:32