Javascript firebase.auth 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48592656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
firebase.auth is not a function
提问by Eric Conner
I am using Webpack with firebase and firebase-admin.
我正在使用带有 firebase 和 firebase-admin 的 Webpack。
To install firebase I ran
安装firebase我跑了
npm install --save firebase
I am importing firebase using,
我正在使用导入 firebase,
import * as firebase from 'firebase/app'
import 'firebase/auth'
I also tried
我也试过
import * as firebase from 'firebase'
And I tried
我试过
const firebase = require('firebase')
As suggested in web get started guide
正如网络入门指南中所建议的
When I try to use firebase.auth()however I get an error
firebase.auth()但是当我尝试使用时出现错误
console.js:32 TypeError: firebase.auth is not a function
console.js:32 TypeError: firebase.auth 不是函数
When I use the debugger to inspect firebaseI see that it in fact does not have an authfunction:
当我使用调试器进行检查时,firebase我发现它实际上没有auth功能:
> firebase
{__esModule: true, initializeApp: ?, app: ?, Promise: ?,?…}
How can I get auth() included as a function using webpack?
如何使用 webpack 将 auth() 作为函数包含在内?
Thanks.
谢谢。
Edit: This is nota duplicate of the question in the comment. That question refers to a method that is a member of the auth service, not the auth service itself.
编辑:这不是评论中问题的重复。该问题指的是作为身份验证服务成员的方法,而不是身份验证服务本身。
回答by Eric Conner
Ok, fixed this by deleting my node_modulesdirectory and reinstalling everything.
好的,通过删除我的node_modules目录并重新安装所有内容来解决此问题。
Also I'm importing firebase like so:
我也像这样导入firebase:
import firebase from 'firebase'
require('firebase/auth')
I don't know.
我不知道。
ˉ\_(ツ)_/ˉ
回答by Joao Lopes
I know you've sorted your problem but there's no real answer to the original problem.
The problem wasn't with the node_modules, it was with the way that you were importing the component.
我知道你已经解决了你的问题,但原始问题没有真正的答案。问题不在于node_modules,而在于您导入组件的方式。
When you export a component ES6 way you normally do export default () => { console.log('default component export'); };
当您以通常的方式导出组件 ES6 时 export default () => { console.log('default component export'); };
defaultis the keyword here, when you import a component ES6 way like import firebase from 'firebase'it's grabbing the defaultproperty from the exported object.
default是这里的关键字,当您以 ES6 的方式导入组件时,就像import firebase from 'firebase'它default从导出的对象中获取属性一样。
Keeping in mind the above example, here's what you've done wrong.
记住上面的例子,这是你做错了什么。
Using ES6:
使用 ES6:
import * as firebase from 'firebase'
console.log(firebase.auth) // Undefined
console.log(firebase.default.auth) // Function
Using ES5:
使用 ES5:
var firebase = require('firebase')
console.log(firebase.auth) // Undefined
console.log(firebase.default.auth) // Function
Note the .default
请注意 .default
Hope this helps to explain what was wrong in the first place.
希望这有助于首先解释什么是错误的。
回答by Lance Samaria
I kept getting an error that said
我一直收到一个错误说
"TypeError: firebase.auth is not a function"
“类型错误:firebase.auth 不是函数”
I got the auth object to appear and the thing I did differently was install the modules in a different order.
我让 auth 对象出现,我做的不同的是以不同的顺序安装模块。
The first time I installed the modules (this is when the auth object wasn't appearing):
我第一次安装模块时(这是当 auth 对象没有出现时):
// this seems to confuse things with the auth object when installed in this order
$ npm install firebase-admin --save
$ npm install firebase --save
I deleted the npm folder and started from scratch although this time I reversed the installation order:
我删除了 npm 文件夹并从头开始,尽管这次我颠倒了安装顺序:
// for some reason this worked and now I can access the auth object
$ npm install firebase --save
$ npm install firebase-admin --save
I didn't do anything else. I simply reversed the installation order by installing firebase first and firebase-admin second.
我没有做任何其他事情。我只是通过先安装 firebase 再安装 firebase-admin 来颠倒安装顺序。
I hope this works for other people.
我希望这对其他人有用。
回答by abderrezague mohamed
just add >
只需添加 >
import firebase from '@firebase/app';
require('firebase/auth');
into your project
进入你的项目
回答by Gayan Pathirage
Though there are many root causes behind this issue, a simple one like this also could be the case.
虽然这个问题背后有很多根本原因,但也可能是这样一个简单的原因。
I forgot to include the js files for auth and db although I use them inside the JS code.
尽管我在 JS 代码中使用了它们,但我忘记包含 auth 和 db 的 js 文件。
Before the fix;
修复前;
<!-- 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>
After the fix;
修复后;
<!-- 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>
回答by Ayush Aryan
This is because firebase-auth script is not added.
这是因为未添加 firebase-auth 脚本。
First you have to install npm files in your node modules by
首先,您必须通过以下方式在节点模块中安装 npm 文件
npm install firebase --save
npm install firebase-admin --save
Then you have to add the script of firebase.auth after firebase-app script and make sure the version is same.
然后你必须在 firebase-app 脚本之后添加 firebase.auth 的脚本并确保版本相同。
Before fix:
修复前:
<script src="https://www.gstatic.com/firebasejs/7.8.1/firebase-app.js"></script>
AFTER FIX YOU NEED TO ADD BOTH THE SCRIPT AND AFTER THIS ADD YOUR FIREBASE ACCOUNT SCRIPT AS FOLLOWS
修复后,您需要添加脚本,然后添加您的 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>
THEN IT SHOULD WORK FINE
那么它应该可以正常工作
回答by Gerardlamo
When running in Node and needing both firebaseand firebase-adminthis works for me:
在 Node 中运行并需要两者时firebase,firebase-admin这对我有用:
First install firebaseand then firebase-adminin that order.
首先安装firebase,然后firebase-admin按该顺序安装。
Then use as so:
然后像这样使用:
const app = require('@firebase/app');
require('@firebase/auth');
require('@firebase/firestore');
const firebase = app.firebase
firebase.initializeApp({…})
const auth = firebase.auth()
const db = firebase.firestore()
回答by CenyGG
Had the same issue, I think it's because of versions troubles.
I solve it by deleting node_modulesand all webpackgenerated stuff and take versions from here.
Btw, I think it's very strange behavior, because it should work like in official documentation.
有同样的问题,我认为是因为版本问题。我通过删除node_modules和所有webpack生成的东西来解决它,并从这里获取版本。
顺便说一句,我认为这是非常奇怪的行为,因为它应该像在官方文档中一样工作。
回答by Troy
I ran into this as well. My issue was the npm module @firebase was installed as well as the firebase module. When I required firebase in my JavaScript code with ‘require(“firebase”)', webpack bundled @firebase instead for some reason.
我也遇到了这个。我的问题是安装了 npm 模块 @firebase 以及 firebase 模块。当我在 JavaScript 代码中使用 'require(“firebase”)' 需要 firebase 时,webpack 出于某种原因捆绑了 @firebase。
@firebase doesn't include auth, database etc. by default...it's modular so you can require them separately. Consequently I received the above error when I tried calling auth().
默认情况下,@firebase 不包括身份验证、数据库等……它是模块化的,因此您可以单独要求它们。因此,当我尝试调用 auth() 时收到了上述错误。
To fix it you can remove @firebase...or just add the full path to the correct firebase when you require it like
要修复它,您可以删除@firebase ...或者在需要时将完整路径添加到正确的firebase
require(‘/path/to/node_modules/firebase/firebase.js')
require('/path/to/node_modules/firebase/firebase.js')
回答by NewbieAid
What-up. I ran into this while working through William Candillon's Adding Firebase To React Nativetutorial...
怎么了。我在阅读 William Candillon 的 Add Firebase To React Native教程时遇到了这个问题......
Thoughts: There is a lot to love about Firebase. But the import/export, named vs default and versioning seems to bring a lot of people a lot of unnecessary heart-ache. <-- I say that with tears streaming down my face and a hole in my heart where a love of mobile development & unhappy childhood used to exist mere hours ago.
想法:Firebase 有很多值得爱的地方。但是导入/导出,named vs default和versioning似乎给很多人带来了很多不必要的心痛。<-- 我说这话时泪流满面,心里有一个洞,几个小时前,对移动开发的热爱和不幸的童年曾经存在过。
Put simply: I had firebase.auth is not a function.
Went hunting though node_modules, deleted, re-yarn'd, read blogs, tried importing as named then default, requiring separate modules a-la require('firebase/auth');under the default import of firebase itself etc etc etc (it really shouldn't be this hard). Also, why do Google not have react documentation? It's 2018. Are people seriously still putting HTML script tags in their front end?
简单地说:我有firebase.auth is not a function。通过 node_modules 寻找,删除,重新编织,阅读博客,尝试按命名然后require('firebase/auth');默认导入,在 firebase 本身等的默认导入下需要单独的模块 a-la (它真的不应该这么难)。另外,为什么谷歌没有反应文档?现在是 2018 年。人们还在认真地将 HTML 脚本标签放在他们的前端吗?
Current Solution => in the end I pulled all my config and firebase.initializeApp(config)into my top level app.js. I'll have to find time later to figure out why the "@firebase" module of auth can't be imported. Or why thats even there? Do i need it? Why isn't it all wrapped into the 'yarn add firebase' module?
当前解决方案 => 最后,我将所有配置拉firebase.initializeApp(config)入我的顶级 app.js。以后我得找时间弄清楚为什么无法导入auth的“@firebase”模块。或者为什么那甚至在那里?我需要吗?为什么不全部包装到“纱线添加火力基地”模块中?
Anyway - that'd be my advice. Get it working at top level first. Then hive-off the credentials into a separate file later. That and "Dont drink lager. It bloats-you-out and IPA is infinitely nicer."
无论如何 - 这就是我的建议。首先让它在顶级工作。然后稍后将凭据组合到一个单独的文件中。那个和“不要喝啤酒。它会让你膨胀,IPA 更好。”

