sveltejs / sveltejs/kit

`browser` check for dynamic server module throws error whilst building.

Open
#14,010 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
20.8k
Forks
2.3k
Avg merge
1d 16h
Merged PRs (30d)
156

Description

Describe the bug

I have a class that helps me manage specific database items, and this class dynamically imports from either the client side database stuff, or server-side (like administration level database stuff). This totally works fine in npm run dev but when it comes to npm run build, the code doesn't build and throws an error, even though it's behind a browser check and none of the server side stuff is saved, only passed on.

Image
Reproduction

Make functions that dynamically import server side modules or client side modules depending on the browser check and then run that function both on the server and the client.

Logs
[vite-plugin-sveltekit-compile] [vite-plugin-sveltekit-guard] Cannot import $lib/server/firebase.ts into client-side code:
- src/routes/[companyId=firestoreid]/+page.svelte imports
,  - $lib/classes/items/certification.ts imports
,    - $lib/classes/items/item.svelte.ts dynamically imports
   - $lib/server/firebase.ts
[vite-plugin-sveltekit-guard] Cannot import $lib/server/firebase.ts into client-side code:
- src/routes/[companyId=firestoreid]/+page.svelte imports
,  - $lib/classes/items/certification.ts imports
,    - $lib/classes/items/item.svelte.ts dynamically imports
   - $lib/server/firebase.ts
    at follow (file:///Users/x/Developer/x/app/node_modules/@sveltejs/kit/src/exports/vite/graph_analysis/index.js:65:10)
    at follow (file:///Users/x/Developer/x/app/node_modules/@sveltejs/kit/src/exports/vite/graph_analysis/index.js:76:5)
    at follow (file:///Users/x/Developer/x/app/node_modules/@sveltejs/kit/src/exports/vite/graph_analysis/index.js:72:5)
    at follow (file:///Users/x/Developer/x/app/node_modules/@sveltejs/kit/src/exports/vite/graph_analysis/index.js:72:5)
    at follow (file:///Users/x/Developer/x/app/node_modules/@sveltejs/kit/src/exports/vite/graph_analysis/index.js:72:5)
    at Object.check (file:///Users/x/Developer/x/app/node_modules/@sveltejs/kit/src/exports/vite/graph_analysis/index.js:84:4)
    at file:///Users/x/Developer/x/app/node_modules/@sveltejs/kit/src/exports/vite/index.js:575:12
    at Array.forEach (<anonymous>)
    at Object.handler (file:///Users/x/Developer/x/app/node_modules/@sveltejs/kit/src/exports/vite/index.js:570:25)
    at file:///Users/x/Developer/x/app/node_modules/rollup/dist/es/shared/node-entry.js:20812:40
System Info
System:
    OS: macOS 15.5
    CPU: (11) arm64 Apple M3 Pro
    Memory: 106.14 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.13.1 - /usr/local/bin/node
    npm: 10.5.2 - /usr/local/bin/npm
  Browsers:
    Edge: 138.0.3351.83
    Safari: 18.5
  npmPackages:
    @sveltejs/adapter-vercel: ^5.6.2 => 5.6.3 
    @sveltejs/kit: ^2.17.2 => 2.19.1 
    @sveltejs/vite-plugin-svelte: ^4.0.0 => 4.0.4 
    svelte: ^5.28.2 => 5.28.2 
    vite: ^5.4.18 => 5.4.18
Severity

annoyance

Additional Information

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Vite graph analysis entry point shown in node_modules/@sveltejs/kit/src/exports/vite/graph_analysis/index.js and reproduce the issue with npm run build using dynamic imports selected by a browser check. Trace why $lib/server/firebase.ts is considered part of the client graph, then verify that the reported client/server import scenario builds correctly without allowing the server module into client-side code.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, vite
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.