npmsvelte95% confidence\u2191 33

ReferenceError: document is not defined in Svelte 3

Full error message
I'm trying in the <script> to manually document.createElement and then to appendChild an audio every time an eventListener is called to replace it. Everything works fine in the browser, apart a really quick error when the page loads but this lasts less then 100ms. There is an error in the Terminal as well 

ReferenceError: document is not defined
    at Object (webpack:///./src/components/Record/Component.svelte?:26:17)

Seems that the above is called when document is not ready yet but afterwards it is fine, how to fix it? Or what is the preferred way to destroy and recreate components in Svelte world (Sapper)?

document is not defined on the server, so you need to guard against that in your component so that particular piece of code is only run in the browser. You can use the onMount function which is only run in the browser when the component has been rendered. <script> import { onMount } from 'svelte'; onMount(() => { document.createElement(...); // ... }); </script>

API access

Get this solution programmatically \u2014 free, no authentication.

curl https://depscope.dev/api/error/32c306d220aebab556b551ecfbeddd9cf6aa9f3a6ed80e59c6cd911ce1c80515
hash \u00b7 32c306d220aebab556b551ecfbeddd9cf6aa9f3a6ed80e59c6cd911ce1c80515
ReferenceError: document is not defined in Svelte 3 — DepScope fix | DepScope