[英]Nuxt.js SSR with firebase - ReferenceError: navigator is not defined
I've made a simple web app with Nuxt(SSR) and firebase(firestore and hosting).我用 Nuxt(SSR)和 firebase(firestore 和托管)制作了一个简单的 web 应用程序。 It has some pages like index, about, contact and text editor.
它有一些页面,如索引、关于、联系和文本编辑器。
When I do npm run build
, it works with no problem.当我执行
npm run build
时,它可以正常工作。 But for npm run generate
, it has an error message like:但是对于
npm run generate
,它有如下错误信息:
ReferenceError: navigator is not defined
at Object.areCookiesEnabled (C:\app\node_modules\@firebase\util\dist\index.node.cjs.js:656:5)
at warnOnBrowserContextMismatch (C:\app\node_modules\@firebase\analytics\dist\index.cjs.js:1072:15)
at factory (C:\app\node_modules\@firebase\analytics\dist\index.cjs.js:1086:5)
at Component.instance.INTERNAL.registerComponent.component.Component.setServiceProps.settings [as instanceFactory] (C:\app\node_modules\@firebase\analytics\dist\index.cjs.js:1165:16)
at Provider.getOrInitializeService (C:\app\node_modules\@firebase\component\dist\index.cjs.js:222:39)
at Provider.getImmediate (C:\app\node_modules\@firebase\component\dist\index.cjs.js:120:33)
at FirebaseAppImpl._getService (C:\app\node_modules\@firebase\app\dist\index.node.cjs.js:230:49)
at FirebaseAppImpl.firebaseAppImpl.<computed> [as analytics] (C:\app\node_modules\@firebase\app\dist\index.node.cjs.js:442:39)
at Object.serviceNamespace [as analytics] (C:\app\node_modules\@firebase\app\dist\index.node.cjs.js:422:45)
at Object.<anonymous> (plugins/firebase.js:22:0)
at __webpack_require__ (webpack/bootstrap:25:0)
at Module.<anonymous> (server.js:2766:16)
at __webpack_require__ (webpack/bootstrap:25:0)
at Object.<anonymous> (server.js:639:18)
at __webpack_require__ (webpack/bootstrap:25:0)
at server.js:118:18
This message actually has been showing up occasionally when I run the app in dev mode and it usually disappeared when I refresh.当我在开发模式下运行应用程序时,这个消息实际上偶尔会出现,当我刷新时它通常会消失。
Here's what I did to fix it:这是我为修复它所做的:
npm install
again.npm install
。 I even deleted the firebase-related files and folder and re-install firebase tools and init again. I looked up dozens of articles on here or other forums, and none of it actually helps to figure out what I'm doing wrong.我在这里或其他论坛上查找了数十篇文章,但实际上并没有帮助找出我做错了什么。
This problem seems related to node or server-side things, but I'm quite new to Vue and also Node, so I couldn't fully understand what I have to do and where I should modify the code.这个问题似乎与节点或服务器端的事情有关,但我对 Vue 和 Node 都很陌生,所以我无法完全理解我必须做什么以及应该在哪里修改代码。
BTW my node is the latest LTS version and vue, nuxt, and all the dependencies are the latest versions as well.顺便说一句,我的节点是最新的 LTS 版本,而 vue、nuxt 和所有依赖项也是最新版本。
Thanks in advance.提前致谢。
Hey dont know if this helps, I ran into the same issue using NextJs.嘿不知道这是否有帮助,我使用 NextJs 遇到了同样的问题。 Here is how I solved it.
这是我解决它的方法。 I wasnt using it in any of my SSR code either though so idk what happened.
我也没有在我的任何 SSR 代码中使用它,但我不知道发生了什么。
const analytics = (): firebase.analytics.Analytics => firebase.analytics();
Then in your code to use it然后在你的代码中使用它
analytics().logEvent();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.