question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

global is not defined (SvelteKit/Vite)

See original GitHub issue
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>

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:14

github_iconTop GitHub Comments

2reactions
Ziaocommented, 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",
    },
  },
2reactions
Swepoolcommented, Apr 15, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix the ReferenceError: global is not defined error in ...
This would usually be fine but SvelteKit doesn't play ball with a Vite config file, it will instead ask you to put the...
Read more >
Vite 'global is not defined' - typescript - Stack Overflow
The problem is because vite doesn't define a global field in window as webpack does. And some libraries relies on it since webpack...
Read more >
Vite global constant replacement not defined #4966 - GitHub
When I try to include a global constant defined in Vite inside of a Svelte file, I get warnings saying that the constant...
Read more >
"global is not defined" when importing Daily.js to SvelteKit
Binding global = window in App.html, or using CDN works! So it's a legacy issue with some JS libraries incorrectly relying on node...
Read more >
global is not defined error in SvelteKit/Vite | JavaScript LibHunt
How to fix the ReferenceError: global is not defined error in SvelteKit/Vite. This page summarizes the projects mentioned and recommended in ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found