firebase.auth不是函数

JavaScript Webpack

神乐

2020-03-24

我将Webpack与firebase和firebase-admin一起使用。

要安装firebase,我跑了

npm install --save firebase

我正在使用导入Firebase,

import * as firebase from 'firebase/app'
import 'firebase/auth'

我也试过

import * as firebase from 'firebase'

我试过了

const firebase = require('firebase')

根据网络入门指南中的建议

firebase.auth()但是,当我尝试使用时,出现错误

console.js:32 TypeError:firebase.auth不是函数

当我使用调试器进行检查时,firebase我发现它实际上没有auth功能:

> firebase
 {__esModule: true, initializeApp: ƒ, app: ƒ, Promise: ƒ, …}

如何使用webpack将auth()包含在函数中?

谢谢。

编辑:这不是注释中问题的重复。该问题涉及的是属于auth服务而不是auth服务本身的成员的方法。

第3549篇《firebase.auth不是函数》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

5个回答
十三 2020.03.24

我遇到了同样的问题,并通过以下方式解决了这个问题:

<script src = "https://www.gstatic.com/firebasejs/6.5.0/firebase-app.js"> </script>
<script src = "https://www.gstatic.com/firebasejs/6.5.0/firebase-auth.js"> </script>
<script>
// Firebase settings of your web application
var firebaseConfig = {
apiKey: "your apikey",
authDomain: "hackatonteleton.firebaseapp.com",
databaseURL: "https://name-database.firebaseio.com",
projectId: "name-projectid",
storageBucket: "name.appspot.com",
messagingSenderId: "730018138942",
Application ID: "1: 730018138942: web: eb12fac2f91fb17f"
};
// Initialize Firebase
firebase.initializeApp (firebaseConfig);
const auth = firebase.auth ();
</script>

您注意到的区别是他们需要:

<script src = "https://www.gstatic.com/firebasejs/6.5.0/firebase-auth.js"> </script>

并初始化功能

const auth = firebase.auth ();`enter code here`
卡卡西 2020.03.24

曾经有过同样的问题,我认为是因为版本问题。我通过删除node_modules所有webpack生成的东西并从此处获取版本来解决它
顺便说一句,我认为这是非常奇怪的行为,因为它应该像官方文档中那样工作。

宝儿理查德 2020.03.24

这是因为未添加firebase-auth脚本。

首先,您必须通过以下方式在节点模块中安装npm文件:

npm install firebase --save
npm install firebase-admin --save

然后,您必须在firebase-app脚本之后添加firebase.auth脚本,并确保版本相同。

修复之前:

<script src="https://www.gstatic.com/firebasejs/7.8.1/firebase-app.js"></script>

修复后,您需要同时添加脚本和此FIREBASE帐户脚本,如下所示

<script src="https://www.gstatic.com/firebasejs/7.8.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.8.1/firebase-auth.js"></script>

然后就应该很好地工作

2020.03.24

我不断收到错误消息,说

“ TypeError:firebase.auth不是函数”

我出现了auth对象,而我做了不同的事情是以不同的顺序安装模块。

第一次安装模块(这是auth对象未出现的时候):

// this seems to confuse things with the auth object when installed in this order
$ npm install firebase-admin --save
$ npm install firebase --save

我删除了npm文件夹并从头开始,尽管这次我颠倒了安装顺序:

// for some reason this worked and now I can access the auth object
$ npm install firebase --save
$ npm install firebase-admin --save

我什么也没做。我只是通过首先安装firebase然后第二安装firebase-admin来颠倒了安装顺序。

我希望这对其他人有用。

你可以在这里读更多关于它的内容

乐古一 2020.03.24

尽管此问题背后有许多根本原因,但也可能是这种情况。

我忘了包括auth和db的js文件,尽管我在JS代码中使用了它们。

修复之前;

<!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/6.1.1/firebase-app.js"></script>

修复后;

<!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/6.1.1/firebase-app.js"></script>

<!-- Add Firebase products that you want to use -->
<script src="https://www.gstatic.com/firebasejs/6.1.1/firebase-auth.js"></script>

<script src="https://www.gstatic.com/firebasejs/6.1.1/firebase-database.js"></script>

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android