0

installing:

npm i peerjs

/plugins/peerjs.js

import Peer from 'peerjs'

export default Peer

nuxt.config.js

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

Browser error:

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

kissu
  • 40,416
  • 14
  • 65
  • 133
E. Williams
  • 405
  • 1
  • 6
  • 21
  • It looks like Peerjs is not supported on the server actually: https://github.com/peers/peerjs/issues/641 So, you should probably do the opposite aka `mode: 'client'`. Also, don't you need to do something else than just `import` and `export` it here. Also, please consider using it locally if you don't need it globally: https://stackoverflow.com/a/69593428/8816585 (and/or https://stackoverflow.com/a/69572014/8816585) – kissu Oct 18 '21 at 14:03
  • changing `ssr: true` to `mode: 'server'` seems to work, idk why – E. Williams Oct 18 '21 at 15:15
  • `ssr` is deprecated and should be replaced by `mode`. Then, `ssr: true` is saying that you want your plugin to be run both on server and on client. Meanwhile, since `peerjs` relies on `window` (only available in the browser), you need to only run it on the `'client'`. Not sure why your thing works with `'server'` tho. Your issue is solved? Mind if I post an answer then? – kissu Oct 18 '21 at 15:24
  • I understand your point, but it crashes on `client` and works on `server` kinda weird – E. Williams Oct 18 '21 at 15:37
  • What is the actual issue? Do you have an error or something? – kissu Oct 18 '21 at 15:39
  • No issues. Just got it working using `{ src: "~/plugins/peerjs.js" , mode:'server'},` instead of the sentence describdeon on question – E. Williams Oct 18 '21 at 15:50

1 Answers1

1

Setting { src: '~/plugins/peerjs.js' , mode: 'server'} fixed the issue.

Strange since it should either be mode: 'client' as shown in the documentation but I guess that PeerJS relies in fact on Node and not on client's window.

kissu
  • 40,416
  • 14
  • 65
  • 133