Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

VS Code does not autocomplete or follow a Vite alias like @/

Errorimport from '@/components/Button' builds fine but is not resolved in the editor

Short answer

Vite and VS Code read different files. Vite reads resolve.alias in the Vite config. VS Code reads paths from jsconfig.json (or tsconfig.json), so both must name the same alias. Put the jsconfig at the project root, and make sure its include covers the files you edit.

  • Applies to Vite 7, Vite 8, VS Code
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check that both files declare the same alias and where the jsconfig sits relative to the code.

    ls jsconfig.json tsconfig.json vite.config.*
    grep -n "alias\|paths" jsconfig.json vite.config.*
  2. 2Match what you see, then jump to its fix

    1. 1No jsconfig.json or tsconfig.json in the projectThe alias exists only in vite.configFix
    2. 2A jsconfig exists but only some folders get IntelliSenseThe jsconfig is in the wrong folder or include skips the filesFix
    3. 3The editor resolves one prefix but Vite resolves another, or the build fails while the editor is happyAlias in Vite and paths in jsconfig disagreeFix

What does the error look like?

  • import from '@/components/Button' builds fine but is not resolved in the editor
  • No autocomplete or go to definition on aliased imports
  • Aliased paths show as unresolved while the app runs

Cause 1 of 3

The alias exists only in vite.config

1Check

Vite's resolve.alias is for the bundler. The editor needs paths in jsconfig.json, which is relative to baseUrl as the VS Code docs say.

ls jsconfig.json

2Fix

Create jsconfig.json at the root of the JavaScript code with the same mapping as the Vite alias.

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["src"]
}

3Verify

Reload the VS Code window and press Ctrl or Cmd with a click on an aliased import.

# Command Palette: Developer: Reload Window

Confirmed by code.visualstudio.com

Did this fix it?

Cause 2 of 3

The jsconfig is in the wrong folder or include skips the files

1Check

A jsconfig.json marks the root of a JavaScript project. Its include and exclude paths are relative to the file, and include defaults to everything in its folder.

git ls-files | grep jsconfig
grep -n "include\|exclude" jsconfig.json

2Fix

Move the file to the project root, or widen include. Exclude build output and node_modules so suggestions do not appear twice.

{
  "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["./src/*"] } },
  "include": ["src", "vite.config.js"],
  "exclude": ["node_modules", "dist"]
}

3Verify

Open a file in each folder and check that aliased imports resolve.

# Command Palette: TypeScript: Restart TS Server

Confirmed by code.visualstudio.com

Did this fix it?

Cause 3 of 3

Alias in Vite and paths in jsconfig disagree

1Check

Vite wants an absolute path in resolve.alias; a relative value is used as written. The jsconfig side stays relative to baseUrl, so write each in its own form and compare the target folder.

grep -n -A4 "alias" vite.config.*

2Fix

Set the Vite alias with an absolute path and keep the jsconfig target pointing at the same folder.

import { fileURLToPath, URL } from 'node:url'
export default {
  resolve: {
    alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) },
  },
}

3Verify

Run a build and check that the editor shows no unresolved import.

npx vite build

Confirmed by vite.dev

Did this fix it?

How do I stop it coming back?

  • Change the alias in vite.config and jsconfig.json in the same commit.
  • Keep one jsconfig per project root; use more only for separate projects in a monorepo.
  • Reload the window after editing jsconfig.json.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Web apps

A POST to a Google Apps Script web app returns 405 or a CORS error

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Apps Script web apps run doPost for POST and doGet for GET, and the Apps Script guides describe no other entry points.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.