geotiff.js icon indicating copy to clipboard operation
geotiff.js copied to clipboard

parcelRequire is not defined when including in a Svelte Rollup application

Open dhatcher opened this issue 3 years ago • 13 comments

I'm attempting to use the library in a svelte application using rollup and getting a parcelRequire error.

Here is my import statement: import * as GeoTIFF from "geotiff/dist-browser/geotiff";

Error message:

Uncaught ReferenceError: parcelRequire is not defined
parcelRequire.QVnC  	geotiff.js:1
(anonymous function)	geotiff.js:1
createCommonjsModule	bundle.js:397
(anonymous function)	bundle.js:8388
(anonymous function)	bundle.js:10333

Any ideas or suggestions would be appreciated.

dhatcher avatar Mar 30 '21 17:03 dhatcher

Hi @dhatcher

Thanks for reporting this issue.

I think in your case, you should not directly reference the bundled library, but reference the source instead. So like that:

import * as GeoTIFF from "geotiff";

In this way rollup can do the bundling instead. Please try that and tell us if that worked.

constantinius avatar Mar 30 '21 17:03 constantinius

I had started with that and ran into this issue in that case:

(!) Missing global variable names
Use output.globals to specify browser global variable names corresponding to external modules
events (guessing 'require$$0$1')
buffer (guessing 'buffer$1')
http (guessing 'http')
https (guessing 'https')
url (guessing 'urlMod')
fs (guessing 'fs')

Those are only warnings but attempting to launch the web site then causes a related failure/error

Uncaught ReferenceError: require$$0$1 is not defined
(anonymous function)	bundle.js:25445

Looking through the other issues led me to try the above.

dhatcher avatar Mar 30 '21 17:03 dhatcher

I'm guessing that the parcelRequire issue is an artifact of using parcel-bundler Perhaps if the migration to the new/supported parcel were done it would be better.

dhatcher avatar Mar 30 '21 19:03 dhatcher

Regarding this:

(!) Missing global variable names
Use output.globals to specify browser global variable names corresponding to external modules
events (guessing 'require$$0$1')
buffer (guessing 'buffer$1')
http (guessing 'http')
https (guessing 'https')
url (guessing 'urlMod')
fs (guessing 'fs')

Those are all node packages, that you'd need to tell your bundler are not available and should be set to null. This must be done for every bundler unfortunately.

I don't quite know how rollup works, especially with es-6, maybe you need something like a babel integration to translate?

constantinius avatar Mar 31 '21 08:03 constantinius

I got this to work with the dist-browser version by making a few edits to it. I mostly had to update the results of parcel-bundler to be more friendly with newer versions of JavaScript.

dhatcher avatar Mar 31 '21 14:03 dhatcher

You mean changes to the dist-browser? This file is generated, so changes made there will not really persist.

constantinius avatar Mar 31 '21 22:03 constantinius

That's correct I made them to the generated file. Like I mentioned earlier I think these issues are something to do with the way parcel-bundler packages the code. Or at least I was able to use the distro once I updated those things. I started to work on switching it to use the newer parcel for packaging but didn't have time and this is at least working for my purposes for now.

If there is a better solution for rollup I'd be happy to use it. I just don't have the time/expertise to look into it further.

dhatcher avatar Apr 01 '21 16:04 dhatcher

I understand. I'm also not very familiar with bundlers. Maybe @PacoDu can help us out here? I think he wrote the switch to parcel.

constantinius avatar Apr 01 '21 20:04 constantinius

Hi, like dhatcher stated, it seems a good idea to upgrade to parcel v2 which should be more suited for library bundling than parcel v1 which wasn't really the "right choice". At the time I did the switch it solved the issues I had using geotiffjs with nodejs, but it seems that it has introduced multiple issues with browser builds.

Maybe bundlers should be re-evaluated before upgrading, parcel v2 claims to be more suited for library building but the documentation is still missing, I've recently heard about microbundler (which is a wrapper around rollup) but I've never used it. Unfortunately I don't have the time right now to investigate the issue. I'll try to take a look later if it's not resolved.

PacoDu avatar Apr 02 '21 12:04 PacoDu

If I recall correctly there is also a worker bundling issue that could be addressed at the same time with the bundler upgrade.

PacoDu avatar Apr 02 '21 12:04 PacoDu

Regarding this specific issue, I would import Geotiff.js from source as @constantinius suggested, this way your rollup bundler should be able to bundle the library from sources and do as it pleased (but you have to deal with nodejs specific modules). The dist-browser is built for direct import in an html file like CDN imports.

Sorry for the sparse responses.

PacoDu avatar Apr 02 '21 14:04 PacoDu

Thanks @PacoDu, your insight is much appreciated! For the next bigger version I'd like to restructure a bit and choosing the right bundler may be on the table again. Okay, will look into parcel v2.

constantinius avatar Apr 02 '21 22:04 constantinius

This same issue seems to replicate when used with nmp@7 workspaces. For some reason when you install geotiff in workspace root node_modules folder it gives error about not finding parcelRequire but when installing normally in application folder it works.

/repo
  package.json  # --> workspace definition
  node_modules/  # --> geotiff breaks here
  package-a/
    package.json  # geotiff required by this package
    node_modules/  # --> geotiff works if it would be installed here
  package-b/

vrtantti avatar Sep 10 '21 06:09 vrtantti