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>