javascript - `Firebase` 包被成功找到。但是,这个包本身指定了一个无法解析的 `main` 模块字段

标签 javascript node.js firebase react-native expo

我正在尝试在 expo 托管的 react-native 应用程序中读取和写入 firestore,使用 firebase 的身份验证和 firebase 的存储。
完全错误:

While trying to resolve module `firebase` from file `C:\Users\joshu\Desktop\VSProjects\VolleyballConnect\firebase.js`, the package `C:\Users\joshu\Desktop\VSProjects\VolleyballConnect\node_modules\firebase\package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`C:\Users\joshu\Desktop\VSProjects\VolleyballConnect\node_modules\firebase\index`. Indeed, none of these files exist:

  * C:\Users\joshu\Desktop\VSProjects\VolleyballConnect\node_modules\firebase\index(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)
  * C:\Users\joshu\Desktop\VSProjects\VolleyballConnect\node_modules\firebase\index\index(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)
我的 Firebase 配置文件:
import firebase from "firebase";
import { initializeApp } from "firebase/app";
import "firebase/firestore";
import "firebase/auth";
import "firebase/storage";

// I'm using the example key here, I have the correct config
const firebaseConfig = {
  apiKey: "api-key",
  authDomain: "project-id.firebaseapp.com",
  databaseURL: "https://project-id.firebaseio.com",
  projectId: "project-id",
  storageBucket: "project-id.appspot.com",
  messagingSenderId: "sender-id",
  appId: "app-id",
  measurementId: "G-measurement-id",
};

if (firebase.apps.length === 0) {
  initializeApp(firebaseConfig);
}

const db = firebase.firestore();
const auth = firebase.auth();
const storage = firebase.storage();

export { db, auth, storage };
我安装了firebase包装:
expo install firebase
任何帮助将不胜感激。谢谢!

最佳答案

为了减小应用程序的大小,firebase SDK (v9.0.0) 变得模块化。您不能再像以前那样在 v8 上执行 import 语句。
你有两个选择。

  • 使用向后兼容的方式。 (稍后将被删除):

  • 这个:
    import firebase from 'firebase/app';
    import 'firebase/auth';
    import 'firebase/firestore';
    
    应改为:
    // v9 compat packages are API compatible with v8 code
    import firebase from 'firebase/compat/app';
    import 'firebase/compat/auth';
    import 'firebase/compat/firestore';
    
  • 现在重构你的代码。

  • 由此:
    import firebase from "firebase/compat/app";
    import "firebase/compat/auth";
    
    const auth = firebase.auth();
    auth.onAuthStateChanged(user => { 
      // Check for user status
    });
    
    对此:
    import { getAuth, onAuthStateChanged } from "firebase/auth";
    
    const auth = getAuth(firebaseApp);
    onAuthStateChanged(auth, user => {
      // Check for user status
    });
    
    你一定要检查documentation

    关于javascript - `Firebase` 包被成功找到。但是,这个包本身指定了一个无法解析的 `main` 模块字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69814654/

    相关文章:

    android - 从 Firebase 数据库获取值(value)

    firebase - firestore 文档字段中可以存储的最长字符串值是多少?

    javascript - 实现 AJAX 后 PHP 验证不起作用

    HTML5 Websocket 脚本在 Ubuntu 中工作,在 Win7 或 WinXP 中不起作用

    javascript - 从 Sequelize 中的另一个表中计数

    node.js - 我怎样才能得到 morgan 的 body react

    android - Firebase A/B 测试版本定位

    javascript - 如何在代码编辑器中打印 fatal error ?

    javascript - 在 AngularJS 中嵌入我个人资料中的 Instagram 最近照片

    javascript - 使用 MySQL Community Server 和 jQuery AJAX 的 RESTful Web 服务