【发布时间】:2022-03-16 21:47:49
【问题描述】:
我正在学习 net ninjas JS 课程,其中有一章是关于使用 firebase 的数据库的。但他的 firebase 代码与现在完全不同,我在 npm 和 node.js 上苦苦挣扎。我按照this 的文档进行操作,我收到“未捕获的语法错误:无法在模块外使用导入语句”。 但是当我将 type="module" 添加到 app.js 时,我得到一个不同的错误“Uncaught TypeError: Failed to resolve module specifier "firebase/app"。相对引用必须以 "/"、"./" 或 " ../"." 以下是我的代码:-
HTML :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
JS:-
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getFirestore, collection, getDocs } from 'firebase/firestore';
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries
// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "AIzaS.....bFkMOXoRY",
authDomain: "modern.....irebaseapp.com",
projectId: "mode....nja",
storageBucket: "modern-j......ja.appspot.com",
messagingSenderId: "9635.....932978",
appId: "1:96350793.......cbf567939ae91512b",
measurementId: "G-HFN5111EHZ"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
所有内容都是从我创建的应用程序中复制粘贴的。请帮忙
【问题讨论】:
标签: javascript firebase npm