Cannot solve "TypeError: firebase__WEBPACK_IMPORTED_MODULE_1__.default.createUserWithEmailAndPassword is not a function" error

Cannot solve "TypeError: firebase__WEBPACK_IMPORTED_MODULE_1__.default.createUserWithEmailAndPassword is not a function" error

我正在尝试使用 firebase 通过我的 React 应用程序创建新用户,不幸的是我似乎一遍又一遍地收到相同的错误消息。

我已阅读线程并似乎按照说明进行操作,但似乎没有任何效果。

我授权 firebase 接受使用电子邮件和密码创建的帐户,并且我使用了 npm install --save firebase

我的firebase.js文件

import firebase from "firebase";
import "firebase/auth";


const firebaseConfig = {
  apiKey: "AIzaSyCHPSB1S58MWb5E5gVXccN1p8DXgFNNuVw",
  authDomain: "pinterest-clone-843c8.firebaseapp.com",
  projectId: "pinterest-clone-843c8",
  storageBucket: "pinterest-clone-843c8.appspot.com",
  messagingSenderId: "1013112040906",
  appId: "1:1013112040906:web:bd97504bf14e75434d3333",
  measurementId: "G-W8VETWB8RF"
};

firebase.initializeApp(firebaseConfig)
const auth = firebase.auth()
export {auth}

我的header.js文件

import {useState} from "react"
import auth from "firebase"
const Header = ({setimageArray}) =>{

auth.createUserWithEmailAndPassword("john@website.com",
"123abbbbc")
.then((userCredential) => {
 
var user = userCredential.user;
console.log(user)

})
.catch((error) => {
  var errorCode = error.code;
  var errorMessage = error.message;
 
  });

我似乎无法摆脱“TypeError: firebase__WEBPACK_IMPORTED_MODULE_1__.default.createUserWithEmailAndPassword is not a function”

感谢您的帮助!

我认为您没有从 firebase.js 文件中正确导入它。尝试将 header.js 中的导入更改为:

import { useState } from "react"
import { auth } from "./firebase" // I asume they are on the same level. If not add more ../