将 peerjs 与 nuxtjs 集成时出错 (vue.js)

Error while integrating peerjs with nuxtjs (vue.js)

正在安装:

npm i peerjs

/plugins/peerjs.js

import Peer from 'peerjs'

export default Peer

nuxt.config.js

plugins: [
  { src: "~/plugins/peerjs.js", ssr: true }
],

浏览器错误:

client.js?06a0:103 TypeError: Cannot set properties of undefined (setting '_events') at i (peerjs.min.js?a0bc:46) at i (peerjs.min.js?a0bc:66) at _callee2$ (index.js?f26e:87) at tryCatch (runtime.js?96cf:63) at Generator.invoke [as _invoke] (runtime.js?96cf:294) at Generator.eval [as next] (runtime.js?96cf:119) at asyncGeneratorStep (asyncToGenerator.js?1da1:3) at _next (asyncToGenerator.js?1da1:25) _callee$ @ client.js?06a0:103 tryCatch @ runtime.js?96cf:63 invoke @ runtime.js?96cf:294 eval @ runtime.js?96cf:119 asyncGeneratorStep @ asyncToGenerator.js?1da1:3 _next @ asyncToGenerator.js?1da1:25 eval @ asyncToGenerator.js?1da1:32 eval @ asyncToGenerator.js?1da1:21 eval @ client.js?06a0:65 Promise.catch (asíncrono) eval @ client.js?06a0:115 eval @ client.js:1294 ./.nuxt/client.js @ app.js:35 webpack_require @ runtime.js:854 fn @ runtime.js:151 0 @ app.js:9767 webpack_require @ runtime.js:854 checkDeferredModules @ runtime.js:46 webpackJsonpCallback @ runtime.js:33 (anónimo) @ app.js:1

设置 { src: '~/plugins/peerjs.js' , mode: 'server'} 解决了这个问题。

奇怪,因为它应该是 mode: 'client',如 the documentation 所示,但我猜想 PeerJS 实际上依赖于 Node 而不是客户端的 window。