simple-peer icon indicating copy to clipboard operation
simple-peer copied to clipboard

global is not defined (SvelteKit/Vite)

Open pjebs opened this issue 2 years ago • 14 comments

global is not defined
ReferenceError: global is not defined
    at node_modules/randombytes/browser.js (http://localhost:3000/node_modules/.vite/simple-peer.js?v=10be15c3:2351:18)
    at __require (http://localhost:3000/node_modules/.vite/chunk-UNANNA3Z.js?v=10be15c3:38:50)
    at node_modules/simple-peer/index.js (http://localhost:3000/node_modules/.vite/simple-peer.js?v=10be15c3:5160:23)
    at __require (http://localhost:3000/node_modules/.vite/chunk-UNANNA3Z.js?v=10be15c3:38:50)
    at http://localhost:3000/node_modules/.vite/simple-peer.js?v=10be15c3:6017:27
    <script>
    	import { SimplePeer} from "simple-peer"
import { browser } from '$app/env';
	
	if (browser) {


 const p = new SimplePeer({
        initiator: location.hash === '#1',
        trickle: false
      })

      p.on('error', err => console.log('error', err))

      p.on('signal', data => {
        console.log('SIGNAL', JSON.stringify(data))
        document.querySelector('#outgoing').textContent = JSON.stringify(data)
      })

      document.querySelector('form').addEventListener('submit', ev => {
        ev.preventDefault()
        p.signal(JSON.parse(document.querySelector('#incoming').value))
      })

      p.on('connect', () => {
        console.log('CONNECT')
        p.send('whatever' + Math.random())
      })

      p.on('data', data => {
        console.log('data: ' + data)
      })

	}

     
    </script>

        <style>
      #outgoing {
        width: 600px;
        word-wrap: break-word;
        white-space: normal;
      }
    </style>

    <form>
      <textarea id="incoming"></textarea>
      <button type="submit">submit</button>
    </form>
    <pre id="outgoing"></pre>

pjebs avatar Apr 08 '22 01:04 pjebs

I also tried import * as SimplePeer from "simple-peer"

pjebs avatar Apr 08 '22 01:04 pjebs

The issue seems to be in the randombytes library

pjebs avatar Apr 08 '22 01:04 pjebs

My solution was to just add the simplepeer.min.js file without importing and then use window.SimplePeer. I couldn't find any other work-around.

pjebs avatar Apr 09 '22 23:04 pjebs

Got the same issue

Swepool avatar Apr 14 '22 22:04 Swepool

I did var global = global || window; in index.html

Swepool avatar Apr 15 '22 10:04 Swepool

@Swepool Your trick didn't work for me. It stopped the global is not defined error. But then SimplePeer gave me this error.

TypeError: Cannot read properties of undefined (reading 'call')
    at Peer.Readable (_stream_readable.js:184:10)
    at new Duplex (_stream_duplex.js:60:12)
    at new Peer (index.js:34:5)
    at gotMedia (caller.svelte? [sm]:14:4)

pjebs avatar Apr 30 '22 22:04 pjebs

Any solution for this so far?

ErcouldnT avatar May 10 '22 22:05 ErcouldnT

@ErcouldnT https://github.com/feross/simple-peer/issues/883#issuecomment-1094142165

pjebs avatar May 16 '22 05:05 pjebs

How do you get this script file?

ErcouldnT avatar May 16 '22 09:05 ErcouldnT

It's in the repo https://github.com/feross/simple-peer/blob/master/simplepeer.min.js

pjebs avatar May 16 '22 11:05 pjebs

Fixed with

https://stackoverflow.com/questions/68975837/web3js-fails-to-import-in-vue3-composition-api-project/69021714#69021714

machester4 avatar Jun 24 '22 23:06 machester4

This worked for me:

import SimplePeer from 'simple-peer/simplepeer.min.js'

wmcmurray avatar Oct 07 '22 19:10 wmcmurray

In short, when using Vite, this is the fix until this gets properly addressed.

In your vite.config.ts, add a resolve section with the following content. This tells the bundler to use the pre-bundled SimplePeer code whenever you import it. As a bonus, you get to keep the types, when using Typescript.

resolve: {
    alias: {
      // ...
      "simple-peer": "simple-peer/simplepeer.min.js",
    },
  },

Ziao avatar Nov 05 '22 20:11 Ziao

It worked elegantly. Thx : )

AAAAsea avatar Nov 12 '22 08:11 AAAAsea