geotiffjs / geotiff.js

geotiff.js is a small library to parse TIFF files for visualization or analysis. It is written in pure JavaScript, and is usable in both the browser and node.js applications.
https://geotiffjs.github.io/
MIT License
845 stars 175 forks source link

Support also CommonJS #425

Closed jfoclpf closed 4 months ago

jfoclpf commented 4 months ago

The documentation mentions

geotiff.js works with both require, import and the global variable GeoTIFF:

Though it's not working with require:

const GeoTIFF = require('geotiff');
const { fromUrl, fromUrls, fromArrayBuffer, fromBlob } = GeoTIFF;
/home/joao/dev/geoapi.pt/resources/node_modules/geotiff/dist-node/source/blockedsource.js:7
const quick_lru_1 = __importDefault(require("quick-lru"));
                                    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/joao/dev/geoapi.pt/resources/node_modules/geotiff/node_modules/quick-lru/index.js from /home/joao/dev/geoapi.pt/resources/node_modules/geotiff/dist-node/source/blockedsource.js not supported.
Instead change the require of index.js in /home/joao/dev/geoapi.pt/resources/node_modules/geotiff/dist-node/source/blockedsource.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/joao/dev/geoapi.pt/resources/node_modules/geotiff/dist-node/source/blockedsource.js:7:37)
    at Object.<anonymous> (/home/joao/dev/geoapi.pt/resources/node_modules/geotiff/dist-node/source/remote.js:6:28)
    at Object.<anonymous> (/home/joao/dev/geoapi.pt/resources/node_modules/geotiff/dist-node/geotiff.js:33:21)
    at Object.<anonymous> (/home/joao/dev/geoapi.pt/resources/altimetry/test.js:1:17) {
  code: 'ERR_REQUIRE_ESM'
}
$ node -v
v16.20.2
$ npm -v
8.19.4

Apparently is one of your dependencies, named quick-lru

constantinius commented 4 months ago

@jfoclpf

quick-lru is listed in the dependencies. Are you sure you properly installed geotiff.js?

jfoclpf commented 4 months ago

@jfoclpf

quick-lru is listed in the dependencies. Are you sure you properly installed geotiff.js?

yes, via npm install geotiff

then I used require

jfoclpf commented 4 months ago

I found this https://nodejs.org/api/esm.html#import-expressions

it should be possible to run

import('geotiff').then(module => { })

But I still need to figure out for this specific case

jfoclpf commented 4 months ago

If others stumble with this problem, that's how I solved it, with dynamic import

const lerp = (a, b, t) => (1 - t) * a + t * b

function transform(a, b, M, roundToInt = false) {
  const round = (v) => (roundToInt ? v | 0 : v);
  return [
    round(M[0] + M[1] * a + M[2] * b),
    round(M[3] + M[4] * a + M[5] * b),
  ];
}

(async () => {
  const fs = require('fs')
  const { fromUrl, fromArrayBuffer, fromBlob } = await import('geotiff')

  // Load our data tile from url, arraybuffer, or blob, so we can work with it:
  const arrBuffer = fs.readFileSync('tif/Vs6UB-2019_2__DEM1__coverage_20240326220529.tif').buffer
  const tiff = await fromArrayBuffer(arrBuffer)
  const image = await tiff.getImage(); // by default, the first image is read.

  /* ... */

  console.log(`The elevation at (${lat.toFixed(6)},${long.toFixed(6)}) is ${elevation}m`);
})()
Burstaholic commented 1 week ago

This workaround does not succeed in my project, possibly due to using a newer version of NodeJS Node.js v20.10.0