global is not defined (SvelteKit/Vite)
See original GitHub issueglobal 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:
- Created a year ago
- Reactions:3
- Comments:14
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.I did
var global = global || window;
in index.html