svelte-kit-monorepo-alias-resolving-bug Svelte Themes

Svelte Kit Monorepo Alias Resolving Bug

It is the reproduction of svelte-kit bug with resolving $lib aliases

svelte-kit-monorepo-alias-resolving-bug

Monorepo setup

This repository reproduces the issue described in here.
It’s a pnpm monorepo created with npx sv create my-app and slightly adjusted.

The repo contains two packages:

  • @web/app — the SvelteKit application (the one we build).
  • @web/utils — a utility package that exports plain TypeScript files.

Build command (from the monorepo root):

pnpm --filter @web/app build

@web/utils exports two functions from index.ts:

  • One defined directly in index.ts
  • One re-exported from temp.ts

Problem

When building @web/app as-is, the build fails because $lib/temp.js from @web/utils is incorrectly resolved as <...>/packages/app/src/lib/temp.js. In other words, $lib paths are always resolved relative to the SvelteKit app (@web/app), instead of the correct package where the file actually lives.

If we add vite-tsconfig-paths plugin, it should correctly resolve aliases based on each package’s tsconfig.However, it does not work until we manually remove the $lib alias that SvelteKit injects into the Vite config.

Why it happens

The @sveltejs/kit/vite plugin always forces $lib into Vite’s resolve.alias. According to Vite’s docs, resolve.alias is passed to Rollup’s alias plugin. Rollup’s alias plugin performs a simple string replacement, without considering tsconfig. Because this happens before vite-tsconfig-paths runs, all $lib imports are already replaced with incorrect paths.

Top categories

Loading Svelte Themes