next.js 未找到模块:包路径.未从包中导出

8nuwlpux  于 2022-12-23  发布在  其他
关注(0)|答案(3)|浏览(215)

尝试连接Firebase时,我的代码中出现以下错误。
文件:firebase. js
代码:

import * as firebase from "firebase"

const firebaseConfig = {
  apiKey: "***",
  authDomain: "***",
  projectId: "***",
  storageBucket: "***",
  messagingSenderId: "***",
  appId: "***",
};

const app = !firebase.apps.length
  ? firebase.initializeApp(firebaseConfig)
  : firebase.app();

const db = app.firestore();
const auth = app.auth();
const provider = new firebase.auth.GoogleAuthProvider();

export { db, auth, provider };

错误:

Module not found: Package path . is not exported from package <project location>\node_modules\firebase (see exports field in <project location>\node_modules\firebase\package.json)
Did you mean './firebase'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.
> 1 | import * as firebase from "firebase";
  2 |
  3 | const firebaseConfig = {
  4 |   apiKey: "***",

Import trace for requested module:
./pages\_app.js

https://nextjs.org/docs/messages/module-not-found

文件:./pages_app. js

import "../styles/globals.css";
import { useAuthState } from "react-firebase-hooks/auth";
import { auth, db } from "../firebase";
import Login from "./login";

function MyApp({ Component, pageProps }) {
  const [user] = useAuthState(auth);

  if (!user) return <Login />;

  return <Component {...pageProps} />;
}

export default MyApp;
qyyhg6bp

qyyhg6bp1#

您应该将firebase导入更改为:

import * as firebase from "firebase"
hyrbngr7

hyrbngr72#

您应该按照错误消息中的指示将'firebase'替换为'./firebase'

sbtkgmzw

sbtkgmzw3#

我也遇到过同样的问题。你要做的就是打电话
第一个月
而不是import * as firebase from "firebase"
此外,如果您尝试使用所有函数(如GoogleAuthProvider),则必须按如下方式导入它
import { getAuth, GoogleAuthProvider } from "firebase/auth";
希望这能帮上忙。

相关问题