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
- Set
resolve: { tsconfigPaths: true } in vite config
- Define path aliases in tsconfig.json (e.g.,
"~/*": ["./src/*"])
- Import using a tsconfig path alias in SSR code (e.g.,
import { foo } from '~/utils')
- 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)
Description
fetchModuleinpackages/vite/src/node/ssr/fetchModule.tshardcodestsconfigPaths: falsewhen callingtryNodeResolvefor SSR module resolution:https://github.com/vitejs/vite/blob/main/packages/vite/src/node/ssr/fetchModule.ts#L63
This means that even when
resolve.tsconfigPaths: trueis set in the Vite config, SSR module fetching completely ignores tsconfig path mappings.Expected behavior
fetchModuleshould respect the user'sresolve.tsconfigPathsconfig value (environment.config.resolve.tsconfigPaths) instead of hardcodingfalse.Reproduction
resolve: { tsconfigPaths: true }in vite config"~/*": ["./src/*"])import { foo } from '~/utils')Workaround
We currently work around this with a custom Vite plugin that manually parses tsconfig.json and resolves paths via
resolveId:Suggested fix
Or if there's a specific reason it was disabled for SSR, it would be great to understand why and document it.
Environment