gpt4 book ai didi

javascript - 未捕获的 TypeError : Failed to resolve module specifier "firebase/app". 相对引用必须以 "/"、 "./"或 "../"开头

转载 作者:行者123 更新时间:2023-12-05 00:31:33 25 4
gpt4 key购买 nike

我已经关注了关于 WebRTC 视频聊天的 YouTube 教程,所以我尝试编写它。在 localhost 中它可以工作,但是一旦我将它上传到 firebase 托管它就会出现此错误。
我能做些什么?我是网络开发新手,请耐心等待
主要.hmtl

<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>WebRtc Demo</title>
</head>
<body>

<h2>Start Cam</h2>
<div class ="videos">
<span>
<h3>Local</h3>
<video id="webcamVideo" autoplay playsinline></video>
</span>

<span>
<h3>Remote</h3>
<video id="remoteVideo" autoplay playsinline></video>
</span>
</div>

<button id="webcamButton">Start cam</button>

<h2>New Call</h2>
<button id="callButton">call</button>

<input id="callInput"/>

<button id="answerButton">Rispondi</button>
<button id="hangupButton">Hangup</button>

<script type="module" src="/main.js"></script>


</body>
</html>
Firebase.json
  "firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"hosting": {
"public": "/",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
main.js
import firebase from 'firebase/app';
import 'firebase/firestore';
const firebaseConfig = {
...
};

最佳答案

我遇到过同样的问题。我会告诉你我是如何解决的:
首先尝试使用 CDN 从谷歌服务器导入 firebase 模块。看这里:

     import { initializeApp } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-app.js";
import { getFirestore, doc, getDoc, getDocs, collection } from "https://www.gstatic.com/firebasejs/9.4.0/firebase-firestore.js";
这很好用。但为了提高效率,我猜你想使用 npm 在你的计算机上加载 firebase 模块。
在这种情况下,您绝对需要像 Webpack、Rollup、Parcel、Snowpack 等模块捆绑器...我使用了 网页包 ,他们有很多关于它的教程。
如果您使用的是模块捆绑器,它会将您的 main.js 文件编译成另一个文件,我们称之为 bundle.js。
然后解决这个问题:
<script type="module" src="/main.js"></script>
至 :
<script type="module" src="dist/bundle.js"></script>
或(取决于您如何设置路径)
<script type="module" src="bundle.js"></script>
所以你得到这个错误是因为你的 main.js 没有编译。
我希望这对你有帮助。和平

关于javascript - 未捕获的 TypeError : Failed to resolve module specifier "firebase/app". 相对引用必须以 "/"、 "./"或 "../"开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66644782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com