Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

320
Vistas
Not able to Initialize Firebase APP inside script module in HTML

I am working on a minimal (proof of concept) project with Preact and Firebase.

A single HTML file, bundler/transpiler free thanks to HTM as JSX replacement.

Here is the relevant code:

            useEffect(() => {
                let isSuscribed = true
                const initFirebase = async () => {

                    let initApp = initializeApp || (await import('https://www.gstatic.com/firebasejs/9.6.10/firebase-app.js')).initializeApp
                    if (!initializeApp) {
                        setInitializeApp(initApp)
                    }


                    let getDB = getFirestore || (await import('https://www.gstatic.com/firebasejs/9.6.10/firebase-firestore.js')).getFirestore
                    if (!getFirestore) {
                        setGetFirestore(getDB)
                    }

                    const fireApp = initApp(firebaseConfig)
                    const appDB = getDB(fireApp)

                    if (isSuscribed) {
                        setFirebase(fireApp)
                        setDB(appDB)
                    }
                }
                if (firebaseConfig)
                    initFirebase()
                        .catch(e => {
                            console.log(e);
                        })

                return () => isSuscribed = false
            }, [firebaseConfig])

When firebaseConfig (JSON file) is loaded from an <input type="file"/>, it successfully imports initializeApp and getFirestore from the provided CDN.

But no matter if I use firebaseConfig data or directly the project id it returns:

FirebaseError: "projectId" not provided in firebase.initializeApp.

This doesn't work either:

initApp({ projectId: 'project-id' })

All is inside a <script type="module"> tag.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Problem solved.

I think the proxy variable initApp was causing the issue. I changed the way initializeApp was being imported, and it did the trick.

            useEffect(() => {
                let isSuscribed = true
                const initFirebase = async () => {
                    try {
                        const [
                            { initializeApp },
                            { getFirestore, collection, addDoc }
                        ] = await Promise
                            .all([
                                import('https://www.gstatic.com/firebasejs/9.6.10/firebase-app.js'),
                                import('https://www.gstatic.com/firebasejs/9.6.10/firebase-firestore.js')
                            ])

                        const fireApp = initializeApp(firebaseConfig)
                        const appDB = getFirestore(fireApp)

                        if (isSuscribed) {
                            setFirebase(fireApp)
                            setDB(appDB)
                            setResult('success')
                        }
                    } catch (e) {
                        console.log(e);
                        setResult('error')
                    }
                }

                if (firebaseConfig) {
                    initFirebase()
                        .catch(e => {
                            console.log(e);
                        })
                }

                return () => isSuscribed = false
            }, [firebaseConfig])
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda