nklayman / vue-cli-plugin-electron-builder

Easily Build Your Vue.js App For Desktop With Electron
https://nklayman.github.io/vue-cli-plugin-electron-builder/
MIT License
4.12k stars 278 forks source link

Firebase not working: Fetch is not defined #1645

Open permafrost06 opened 2 years ago

permafrost06 commented 2 years ago

Describe the bug I'm trying to use firebase firestore with background.js but keep getting error saying fetch is not defined. The firebase js SDK is supposed to work in both web and node.js. In this case though, it seems firebase does not recognize that it's running on node.js and instead tries to run as it does on web. But it fails since node.js does not have web APIs such as fetch. I have tested that firebase firestore runs as intended in a standalone node.js script. I have tried putting firebase and firestore in the externals but it didn't help. (Changing some things show a different error headers is not defined in a error window)

To Reproduce

Expected behavior Expect firestore to behave normally and fetch a doc from the server.

Environment (please complete the following information):

Additional context From the research I've done on this, it seems there might be an issue with webpack configuration. But addingfirebase, firebase\firestore, and firebase\firestore\lite or @firebase\firestore and @firebase\firestore\lite to vue.config.js's pluginOptions.electronBuilder.externals hasn't changed anything.

nklayman commented 2 years ago

Try installing the @firebase/app and @firebase/firestore packages, then set your externals to externals: ['@firebase/app', '@firebase/firestore/lite']. Then, import from those packages like so:

import { initializeApp } from "@firebase/app";
import {
   getFirestore,
   getDoc,
   updateDoc
} from '@firebase/firestore/lite';
permafrost06 commented 2 years ago

Trying to import firestore using import { initializeFirestore } from "firebase/firestore/lite" or import { initializeFirestore } from "@firebase/firestore/lite" fails with error:

 ERROR  Failed to compile with 1 errors                                                                       1:34:57 PM

This dependency was not found:

* firebase/firestore/lite in ./src/background.js

To install it, you can run: npm install --save firebase/firestore/lite
nklayman commented 2 years ago

Can you share a repo that demonstrates the error? Using that import worked fine for me so I'm not sure why it won't work for you.

permafrost06 commented 2 years ago

Check out this repo: vue-electron-firestore Running npm run electron:serve results in an error window: image

cfjohan commented 2 years ago

I'm trying to use the compat packages using the docs on this page.

I'm getting the same error "fetch is not defined" and installing the @firebase/app and @firebase/firestore packages doesn't seem to help, I'm guessing those don't include the compat packages. Apparently @firebase/compat/app doesn't exist.

I'm curious as to why the firebase docs suggest importing from firebase/app and not @firebase/app and what the difference is?