sveltejs / sveltejs/kit

src/env.ts resolves linked workspace dependencies differently from installed dependencies

Open
#16,987 0 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

When SvelteKit evaluates src/env.ts, it creates an internal Vite server with configFile: false.

Vite externalizes normal installed dependencies during SSR, but linked workspace dependencies are inlined by default. This means the same dependency can execute with native Node semantics when installed, but inside SvelteKit's isolated Vite runner when linked.

In my case the linked package contains an import resolved by a Node loader hook. It works when executed by Node, but svelte-kit sync fails because the linked package is instead inlined into the internal Vite server, which does not have the project's Vite plugins and does not delegate that import to Node.

I currently patch the internal server with:

ssr: {
external: true
}

This makes the linked dependency external and fixes the issue.

I haven't submitted a PR cause i'm not sure the ssr.external solution is desired. Can do if you'd like though.

Reproduction

https://github.com/ageorgeh/sveltekit-sync-repro

Sorry, bit of a weird repro with the node loader, best I could come up with. In my real project I have a rolldown/vite plugin which does the aliasing and the node loader alongside that for things that aren't bundled so that the behavior is consistent across different sections of the codebase.

Logs
Loading Svelte config from Vite config failed: Error: Cannot find module 'repro-loader-target' imported from '/home/alex/code/sveltekit-sync-repro/packages/env-dependency/index.js'
System Info
System:
    OS: Linux 6.18 cpe:/o:nixos:nixos:26.11 26.11 (Zokor)
    CPU: (16) x64 13th Gen Intel(R) Core(TM) i5-13400F
    Memory: 40.63 GB / 62.65 GB
    Container: Yes
    Shell: 5.3.9 - /run/current-system/sw/bin/bash
  Binaries:
    Node: 24.18.0 - /etc/profiles/per-user/alex/bin/node
    npm: 11.16.0 - /etc/profiles/per-user/alex/bin/npm
    pnpm: 11.11.0 - /etc/profiles/per-user/alex/bin/pnpm
    bun: 1.3.13 - /etc/profiles/per-user/alex/bin/bun
  Browsers:
    Chrome: 150.0.7871.124
    Firefox: 152.0.6
    Firefox Developer Edition: 152.0.6
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 in src/env.ts, where SvelteKit creates the internal Vite server with configFile: false, and compare how installed and linked workspace dependencies are handled during SSR. Run the linked reproduction and verify that svelte-kit sync resolves the dependency consistently with the installed case without relying on the user's patch.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, typescript, vite
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.