Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

global is not defined (SvelteKit/Vite) #883

Open
pjebs opened this issue Apr 8, 2022 · 14 comments
Open

global is not defined (SvelteKit/Vite) #883

pjebs opened this issue Apr 8, 2022 · 14 comments

Comments

@pjebs
Copy link

pjebs commented Apr 8, 2022

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
Copy link
Author

pjebs commented Apr 8, 2022

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

@pjebs
Copy link
Author

pjebs commented Apr 8, 2022

The issue seems to be in the randombytes library

@pjebs pjebs changed the title global is not defined (SvelteKit) global is not defined (SvelteKit/Vite) Apr 9, 2022
@pjebs
Copy link
Author

pjebs commented Apr 9, 2022

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.

@Swepool
Copy link

Swepool commented Apr 14, 2022

Got the same issue

@Swepool
Copy link

Swepool commented Apr 15, 2022

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

@pjebs
Copy link
Author

pjebs commented Apr 30, 2022

@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)

@ErcouldnT
Copy link

Any solution for this so far?

@pjebs
Copy link
Author

pjebs commented May 16, 2022

@ErcouldnT #883 (comment)

@ErcouldnT
Copy link

How do you get this script file?

@pjebs
Copy link
Author

pjebs commented May 16, 2022

@wmcmurray
Copy link

wmcmurray commented Oct 7, 2022

This worked for me:

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

@Ziao
Copy link

Ziao commented Nov 5, 2022

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",
    },
  },

@AAAAsea
Copy link

AAAAsea commented Nov 12, 2022

It worked elegantly. Thx : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants