未找到模块:错误:包路径。不是从包中导出的
Posted
技术标签:
【中文标题】未找到模块:错误:包路径。不是从包中导出的【英文标题】:Module not found: Error: Package path . is not exported from package 【发布时间】:2022-01-23 10:32:59 【问题描述】:import firebase from 'firebase'
const firebaseConfig =
apiKey: "AIzaSyBOK7x5N5UnjY4TDqndzH7l5tvdNIsWFRc",
authDomain: "todo-app-e3cf0.firebaseapp.com",
projectId: "todo-app-e3cf0",
storageBucket: "todo-app-e3cf0.appspot.com",
messagingSenderId: "940016886081",
appId: "1:940016886081:web:91686613f16b1b1f8001c0",
measurementId: "G-JHPC7TP12K"
;
const firebaseApp = firebase.initializeApp(firebaseConfig);
const db = firebase.firestore();
export default db;
错误 未找到模块:错误:包路径。未从包 C:\Users\Sairam\Visual Code\todo-list\node_modules\firebase 导出(请参阅 C:\Users\Sairam\Visual Code\todo-list\node_modules\firebase\package.json 中的导出字段) 您的意思是“./firebase”吗?
【问题讨论】:
【参考方案1】:你误会const db = firebase.firestore();
应该是const db = firebaseApp.firestore();
即使这样做,您也会收到找不到模块的错误。您需要按以下方式导入
import firebase from 'firebase/compat/app';
import 'firebase/compat/firestore';
这对我有用,因为我遇到了同样的问题!
【讨论】:
【参考方案2】:我相信 firebase 最近有很多更新,所以你应该以这种方式更新导入,它的工作方式就像一个魅力。
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
const firebaseConfig =
apiKey: "AIzaSyBOK7x5N5UnjY4TDqndzH7l5tvdNIsWFRc",
authDomain: "todo-app-e3cf0.firebaseapp.com",
projectId: "todo-app-e3cf0",
storageBucket: "todo-app-e3cf0.appspot.com",
messagingSenderId: "940016886081",
appId: "1:940016886081:web:91686613f16b1b1f8001c0",
measurementId: "G-JHPC7TP12K"
;
// Use this to initialize the firebase App
const firebaseApp = firebase.initializeApp(firebaseConfig);
// Use these for db & auth
const db = firebaseApp.firestore();
const auth = firebase.auth();
export auth, db ;
【讨论】:
【参考方案3】:你应该像下面这样导入。我从 firebase 文档中看到了这一点:https://www.npmjs.com/package/firebase
import initializeApp from "firebase/app";
import getFirestore from 'firebase/firestore/lite';
const firebaseConfig =
apiKey: "AIzaSyBOK7x5N5UnjY4TDqndzH7l5tvdNIsWFRc",
authDomain: "todo-app-e3cf0.firebaseapp.com",
projectId: "todo-app-e3cf0",
storageBucket: "todo-app-e3cf0.appspot.com",
messagingSenderId: "940016886081",
appId: "1:940016886081:web:91686613f16b1b1f8001c0",
measurementId: "G-JHPC7TP12K"
;
const firebaseApp = initializeApp(firebaseConfig);
const db = getFirestore(firebaseApp);
export default db;
【讨论】:
我的屏幕因此而空白。我知道这意味着什么!以上是关于未找到模块:错误:包路径。不是从包中导出的的主要内容,如果未能解决你的问题,请参考以下文章