Skip to content

fetchModule hardcodes tsconfigPaths: false, breaking SSR path resolution #21895

Description

@natew

Description

fetchModule in packages/vite/src/node/ssr/fetchModule.ts hardcodes tsconfigPaths: false when calling tryNodeResolve for SSR module resolution:

https://github.com/vitejs/vite/blob/main/packages/vite/src/node/ssr/fetchModule.ts#L63

const resolved = tryNodeResolve(url, importer, {
  mainFields: ['main'],
  conditions: externalConditions,
  externalConditions,
  external: [],
  noExternal: [],
  extensions: ['.js', '.cjs', '.json'],
  dedupe,
  preserveSymlinks,
  tsconfigPaths: false, // <-- hardcoded
  isBuild: false,
  isProduction,
  root,
  packageCache: environment.config.packageCache,
  builtins: environment.config.resolve.builtins,
})

This means that even when resolve.tsconfigPaths: true is set in the Vite config, SSR module fetching completely ignores tsconfig path mappings.

Expected behavior

fetchModule should respect the user's resolve.tsconfigPaths config value (environment.config.resolve.tsconfigPaths) instead of hardcoding false.

Reproduction

  1. Set resolve: { tsconfigPaths: true } in vite config
  2. Define path aliases in tsconfig.json (e.g., "~/*": ["./src/*"])
  3. Import using a tsconfig path alias in SSR code (e.g., import { foo } from '~/utils')
  4. Client build resolves correctly, SSR fails to resolve the import

Workaround

We currently work around this with a custom Vite plugin that manually parses tsconfig.json and resolves paths via resolveId:

{
  name: 'tsconfig-paths-ssr-workaround',
  enforce: 'pre',
  resolveId(source) {
    // manually resolve tsconfig path mappings
  }
}

Suggested fix

- tsconfigPaths: false,
+ tsconfigPaths: environment.config.resolve.tsconfigPaths,

Or if there's a specific reason it was disabled for SSR, it would be great to understand why and document it.

Environment

  • Vite 8.0.0
  • Using framework with SSR (One / vxrn)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions