Vite plugin that enables path resolution using TypeScript's path mapping configuration from tsconfig.json files
npx @tessl/cli install tessl/npm-vite-tsconfig-paths@5.1.1Vite plugin enabling TypeScript path mapping from tsconfig.json. Resolves path aliases and baseUrl, eliminating relative path imports.
Installation: npm install --save-dev vite-tsconfig-paths
import tsconfigPaths from 'vite-tsconfig-paths';
import type { PluginOptions } from 'vite-tsconfig-paths';/**
* Creates Vite plugin for TypeScript path resolution
* @param opts - Configuration options
* @returns Vite Plugin (name: 'vite-tsconfig-paths', enforce: 'pre')
*/
function default(opts?: PluginOptions): Plugin;interface PluginOptions {
/**
* Directory to crawl for tsconfig.json files
* Default: viteConfig.root (with projects) or searchForWorkspaceRoot(viteConfig.root)
*/
root?: string;
/**
* Explicit tsconfig.json paths (disables auto-crawl)
* Absolute or relative to root
*/
projects?: string[];
/**
* Enable path resolution for non-TS/JS files (.vue, .svelte, .mdx)
* Use when allowJs insufficient
*/
loose?: boolean;
/**
* Use TypeScript compiler for parsing (adds ~600ms startup)
* Only for non-standard tsconfig
*/
parseNative?: boolean;
/**
* Suppress tsconfig.json error warnings
* Useful in monorepos
*/
ignoreConfigErrors?: boolean;
/**
* Config file names to search
* Default: ["tsconfig.json", "jsconfig.json"]
*/
configNames?: string[];
/**
* Directory skip predicate (.git, node_modules always skipped)
* Returns true to skip
*/
skip?: (dir: string) => boolean;
}interface TSConfig {
include?: string[];
exclude?: string[];
compilerOptions?: {
baseUrl?: string;
paths?: { [path: string]: string[] };
allowJs?: boolean;
checkJs?: boolean;
outDir?: string;
};
}import { defineConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
plugins: [tsconfigPaths()],
});export default defineConfig({
plugins: [
tsconfigPaths({
root: '../../', // Workspace root
}),
],
});export default defineConfig({
plugins: [
tsconfigPaths({
root: '.',
projects: [
'./packages/app/tsconfig.json',
'./packages/shared/tsconfig.json',
],
}),
],
});import vue from '@vitejs/plugin-vue';
// or: import { svelte } from '@sveltejs/vite-plugin-svelte';
export default defineConfig({
plugins: [
vue(), // or svelte()
tsconfigPaths({ loose: true }),
],
});export default defineConfig({
plugins: [
tsconfigPaths({
skip: (dir) => ['test', 'dist', 'build'].includes(dir),
}),
],
});export default defineConfig({
plugins: [
tsconfigPaths({ ignoreConfigErrors: true }),
],
});{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@components/*": ["components/*"],
"@utils/*": ["utils/*"],
"@/*": ["*"]
}
}
}// Usage
import Button from '@components/Button';
import { formatDate } from '@utils/date';
import { config } from '@/config';{
"compilerOptions": {
"baseUrl": "./src"
}
}// import { config } from 'config';
// Resolves: ./src/config → node_modules/config (fallback){
"include": ["src/**/*"],
"exclude": ["src/**/*.test.ts"]
}Path resolution only applies to matching files.
{
"compilerOptions": {
"allowJs": true,
"baseUrl": "./src",
"paths": { "@/*": ["*"] }
}
}Enables resolution for .vue, .svelte, .mdx, .js, .jsx, .mjs. Use loose: true option if insufficient.
{
"references": [
{ "path": "./packages/core" },
{ "path": "./packages/utils" }
]
}Plugin auto-loads and processes referenced projects.
The plugin operates through Vite's plugin system with these components:
Enable debug logging using the DEBUG environment variable:
DEBUG=vite-tsconfig-paths vite # General logging
DEBUG=vite-tsconfig-paths:resolve vite # Resolution details
DEBUG=vite-tsconfig-paths* vite # All debug outputDebug output includes:
// vite.config.ts
import { defineConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
plugins: [tsconfigPaths()],
});// tsconfig.json
{
"compilerOptions": {
"baseUrl": "./src",
"paths": { "@/*": ["*"] }
}
}// apps/web/vite.config.ts
import { defineConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
plugins: [
tsconfigPaths({
root: '../../', // Auto-discover all tsconfig files in workspace
}),
],
});// vite.config.ts
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import tsconfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
plugins: [
vue(),
tsconfigPaths({ loose: true }), // Enable path resolution in .vue files
],
});// tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/components/*": ["src/components/*"],
"@/composables/*": ["src/composables/*"],
"@/stores/*": ["src/stores/*"]
},
"allowJs": true
}
}// vite.config.ts
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import tsconfigPaths from 'vite-tsconfig-paths';
export default defineConfig({
plugins: [
svelte(),
tsconfigPaths({ loose: true }), // Enable path resolution in .svelte files
],
});baseUrl or paths definedinclude patternsDEBUG=vite-tsconfig-paths:resolve viteloose: true option for non-TypeScript filesroot option to specify workspace rootprojects option to explicitly list tsconfig filesskip option to exclude large directories (improves performance)ignoreConfigErrors if some tsconfig files have issuesskip option to exclude unnecessary directoriesparseNative: true unless absolutely necessary (~600ms startup cost)projects list instead of auto-discovery in very large monoreposPath mapping changes in tsconfig.json require restarting the Vite dev server. The plugin does not hot-reload configuration changes.
\0 in their ID (Vite virtual modules) are not processed