Skip to content

illegal server-only module errors are very bad #14153

@Rich-Harris

Description

@Rich-Harris

Describe the bug

If a universal module/component imports server-only code...

<!-- +page.svelte -->
<script>
  import { getRequestEvent } from '$app/server';
</script>

...you get this error at dev time:

Image

It's annoying that you don't see the import chain until build, and maybe we could fix that. But when you do build, you get this:

Image

There's so much going on here:

  • it's the same freaking error, which still says 'try building your app' to get a better error message
  • it appears twice — once with a [vite-plugin-sveltekit-compile] [vite-plugin-sveltekit-virtual-modules] prefix, once with only a [vite-plugin-sveltekit-virtual-modules] prefix. Huh?
  • it prints the entire file path, instead of one that's relative to your project folder (and hence much more readable)
  • it includes a useless noisy stack trace

Separately: if a dependency in node_modules illegally imports a server-only module, you don't find out until build time. Would be nice if there was a way to detect those a bit earlier, though IIUC Vite doesn't give us the necessary metadata. Something to worry about another time, I guess

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions