azavea / loam

Javascript wrapper for GDAL in the browser
Apache License 2.0
214 stars 16 forks source link

Loam cannot be run under Node.js #94

Open kochizufan opened 2 years ago

kochizufan commented 2 years ago

Hi

I want to use this in node.JS environment on local Windows PC, not on web browser.

Just for test, I wrote such ad-hoc code (without webpack or any packaging) and run:

const loam = require("loam")
loam.initialize()

const main = async () => {

  const file = 'png/Nara-Genpyo.png'
  const dataset = await loam.open(file)
  const width = await dataset.width()
  console.log(width)
}

main()

The result is error follow:

ReferenceError: self is not defined
    at Object.<anonymous> (F:\github\loam\node_modules\loam\lib\loam.js:1:197)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (F:\github\loam\index.js:1:14)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)

I changed the code like this, but still same error occurs.

const main = async () => {
  const loam = await import("loam")
  loam.initialize()

  const file = 'png/Nara-Genpyo.png'
  const dataset = await loam.open(file)
  const width = await dataset.width()
  console.log(width)
}

main()

Is this expected result? Loam don't work in node.js environment or it always must be used with webpack-like packager?

ddohler commented 2 years ago

Hi @kochizufan ! Thanks for trying out the library. Node is not a supported environment for Loam because on Node it is possible to use the GDAL package (https://www.npmjs.com/package/gdal) instead, which should provide the same functionality (and more). So because GDAL is available, I haven't spent time to make Loam work in Node, and it probably doesn't work in Node right now.

However, if you're unable to use the GDAL library then I would be happy to investigate improving Node support. Thanks again!

kochizufan commented 2 years ago

Thank you for explanation. My intension is use wasm version node library with electron. Some library cause some difficulties in packaging phase, especially in case of new architecture like Mac's M1. So I want to use wasm-based library as possible.

ddohler commented 2 years ago

Got it, thanks for the explanation! That makes a lot of sense to me. I will look into what can be done about this (but currently have limited time for the project, so I do not know when I will be able to complete this work; it may be a while, unfortunately).