isomorphic-git / lightning-fs

A lean and fast 'fs' for the browser
MIT License
476 stars 47 forks source link

trying to build on sveltekit; indexedDB is not defined #96

Open codegod100 opened 2 years ago

codegod100 commented 2 years ago
ReferenceError: indexedDB is not defined
    at /home/vera/global-agora/node_modules/.pnpm/@isomorphic-git+idb-keyval@3.3.2/node_modules/@isomorphic-git/idb-keyval/dist/idb-keyval-cjs.js:17:29
    at new Promise (<anonymous>)
    at Store._init (/home/vera/global-agora/node_modules/.pnpm/@isomorphic-git+idb-keyval@3.3.2/node_modules/@isomorphic-git/idb-keyval/dist/idb-keyval-cjs.js:16:21)
    at new Store (/home/vera/global-agora/node_modules/.pnpm/@isomorphic-git+idb-keyval@3.3.2/node_modules/@isomorphic-git/idb-keyval/dist/idb-keyval-cjs.js:10:14)
    at new IdbBackend (/home/vera/global-agora/node_modules/.pnpm/@isomorphic-git+lightning-fs@4.6.0/node_modules/@isomorphic-git/lightning-fs/src/IdbBackend.js:7:19)
    at DefaultBackend.init (/home/vera/global-agora/node_modules/.pnpm/@isomorphic-git+lightning-fs@4.6.0/node_modules/@isomorphic-git/lightning-fs/src/DefaultBackend.js:30:23)
    at PromisifiedFS._init (/home/vera/global-agora/node_modules/.pnpm/@isomorphic-git+lightning-fs@4.6.0/node_modules/@isomorphic-git/lightning-fs/src/PromisifiedFS.js:84:27)
 ELIFECYCLE  Command failed with exit code 1.
jcubic commented 2 years ago

Ok, nice that you have stack trace but this doesn't say anything. Can you at least show the code that generated that error? And what kind of environment do you have?

josh-sachs-epic commented 2 years ago

Having the same issue... Obviously indexedDB doesn't exist on node backend... Store._init is attempting access despite running on node and not the browser.

Is this actually isomorphic, or am I missing something?

jcubic commented 2 years ago

@josh-sachs-epic why would you want to use the browser library in Node? LightingFS is a replacement for the node fs module for the browser. Can you just use the node fs module?

josh-sachs-epic commented 2 years ago

Sure - I want to use FS on node and LightningFS in the browser. Its just that, based on the description, I expected that this lib abstracted that away for me (running the correct bits in node / browser).

I was chiming in on this thread b/c you commented that the submitter's stack trace didn't reveal anything, when in fact it does. They are also assuming the library can handle running in both environments, when it cannot (which is fine - just ambiguity around the use of the term 'isomorphic').

jcubic commented 2 years ago

It's not isomorphic-fs it's a library that can be used in isomorphic-git in the browser.

ninjadev64 commented 8 months ago

Solution is just to initialise FS in onMount.

jcubic commented 8 months ago

@ninjadev64 this will not help, the problem is that the OP has problem with using lightning-fs that was created for Browser outside of the Browser context.

ninjadev64 commented 8 months ago

@ninjadev64 this will not help, the problem is that the OP has problem with using lightning-fs that was created for Browser outside of the Browser context.

I'm using it in the same environment as them and had the same issue. SvelteKit pre-renders and pre-executes Svelte files and JS code in them, both in dev and in deployment. This allows it to send a fully rendered HTML file to the client so that the client doesn't have to wait for it to hydrate on their end.

However, in this process, globals like document and window, as well as browser only technologies like indexedDB, are unavailable. onMount allows the developer to specify a function that is only run when the page is loaded in the browser and these globals have become available.