最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - requiring firebaseUI - window is not defined - Stack Overflow

programmeradmin3浏览0评论

I have a JS file and I'm trying to use Firebase.

This is the part that causes the issue (specifically firebaseUI)

var firebase = require('firebase');
var firebaseui = require('firebaseui');

I didn't forget to install both:

npm install firebase --save
npm install firebaseui --save

But when I run the node I get:

/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/firebaseui/dist/npm.js:30
ponentHandler["register"]=ponentHandler.register;ponentHandler["downgradeElements"]=ponentHandler.downgradeElements;windowponentHandler=ponentHandler;window["ponentHandler"]=ponentHandler;
                                                                                                                                ^

ReferenceError: window is not defined
    at /Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/firebaseui/dist/npm.js:30:129
    at Object.<anonymous> (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/firebaseui/dist/npm.js:420:460)
    at Module._pile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/Users/nimrodshai/Documents/Projects/WeatherJS/JS/server.js:8:18)
    at Module._pile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

If I remove the firebaseUI line, it's all good.

What should I do?

I have a JS file and I'm trying to use Firebase.

This is the part that causes the issue (specifically firebaseUI)

var firebase = require('firebase');
var firebaseui = require('firebaseui');

I didn't forget to install both:

npm install firebase --save
npm install firebaseui --save

But when I run the node I get:

/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/firebaseui/dist/npm.js:30
ponentHandler["register"]=ponentHandler.register;ponentHandler["downgradeElements"]=ponentHandler.downgradeElements;window.ponentHandler=ponentHandler;window["ponentHandler"]=ponentHandler;
                                                                                                                                ^

ReferenceError: window is not defined
    at /Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/firebaseui/dist/npm.js:30:129
    at Object.<anonymous> (/Users/nimrodshai/Documents/Projects/WeatherJS/node_modules/firebaseui/dist/npm.js:420:460)
    at Module._pile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/Users/nimrodshai/Documents/Projects/WeatherJS/JS/server.js:8:18)
    at Module._pile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

If I remove the firebaseUI line, it's all good.

What should I do?

Share Improve this question asked Jan 15, 2019 at 9:51 Nimrod ShaiNimrod Shai 1,17913 silver badges32 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

Firebaseui is meant to run in browser and expects window object to be loaded, if you are using SSR(like next.js) or to be more specific if the code runs in server side and not in the browser it won't find the window object.

I landed here when I was trying to integrate firebaseui with next js, I finally was able to solve the problem using dynamic imports.

Find below the code for Login ponent which loaded the firebase UI.

const LoginFinal = () => {
  useEffect(async () => {
    
    // delay the import until window object is ready
    const firebaseui = await import("firebaseui");
    const ui = new firebaseui.auth.AuthUI(auth);
    ui.start("#firebaseui", {
      signInOptions: [firebaseAuthObject.EmailAuthProvider.PROVIDER_ID],
    });
   
  });

  return <div id="firebaseui" />;
};

I also stumbled across this issue. MeanMan's answer really helped me, but there are two things that needs to be considered:

  • useEffect must not accept an async function, as it returns a promise, which useEffect doesn't support. more info
  • we should take into account that there might be already an instance of AuthUI running, and in that case we should not be creating a new one. Otherwise the error Error: An AuthUI instance already exists for the key "[DEFAULT]" might appear. more info

The final ponent looks like this (using TypeScript)

import React, { FC, useCallback, useEffect } from "react";
import firebase from "firebase";

interface Props {
  firebaseClient: typeof firebase;
  config: firebaseui.auth.Config;
}

const FirebaseUiLogin: FC<Props> = ({ firebaseClient, config }) => {
  const loadFirebaseui = useCallback(async () => {
    const firebaseui = await import("firebaseui");
    const firebaseUi =
      firebaseui.auth.AuthUI.getInstance() ||
      new firebaseui.auth.AuthUI(firebaseClient.auth());
    firebaseUi.start("#firebaseui-auth-container", config);
  }, [firebaseClient, config]);

  useEffect(() => {
    loadFirebaseui();
  }, []);

  return <div id="firebaseui-auth-container"></div>;
};

export default FirebaseUiLogin;
发布评论

评论列表(0)

  1. 暂无评论