Maaz Zindani

Loading the index…

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

TypeScript cannot see useRoute in a Nuxt project

Your error

Cannot find name 'useRoute'.2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The .nuxt type files were never generated

Generate the types and reload the editor window so its language service picks up the new files.

Fix
npx nuxt prepare
npx nuxt typecheck
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Cannot find name 'useRoute' in Nuxt means TypeScript does not see the declarations Nuxt writes into .nuxt for its auto-imports. Run npx nuxt prepare to generate them, keep the root tsconfig.json pointing at the .nuxt configs, and if auto-imports are switched off, import useRoute from '#imports'. Applies to Nuxt 4.6, and to Nuxt 3.21 with its single .nuxt/tsconfig.json.

  • Applies to Nuxt 4.6.4, Nuxt 3.21.11
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Check three things at once: whether .nuxt holds generated tsconfig files, what the root tsconfig.json contains, and whether nuxt.config turns auto-imports off. Missing .nuxt files point to cause 1, a root tsconfig without .nuxt references to cause 2, an imports block to cause 3.

Run this first
ls .nuxt/tsconfig*.json
cat tsconfig.json
grep -n -A3 "imports:" nuxt.config.ts

Then find the line you see in this list and open its fix:

  1. 1The .nuxt type files were never generatedls finds no .nuxt/tsconfig*.json, for example in a fresh clone or a CI job that type-checks before any build
  2. 2The root tsconfig.json no longer points at the generated configsThe .nuxt/tsconfig files exist, but tsconfig.json at the project root has its own compilerOptions and no references or extends entry for .nuxt
  3. 3Auto-imports are turned off in nuxt.confignuxt.config.ts has imports: { autoImport: false }, and other composables such as ref or useFetch fail in the same way

What does the error look like?

  • Cannot find name 'useRoute'.
  • Cannot find module '#imports' or its corresponding type declarations.
  • npx nuxt typecheck fails on useRoute, useFetch or ref in every .vue file

Cause 1 of 3

The .nuxt type files were never generated

You seels finds no .nuxt/tsconfig*.json, for example in a fresh clone or a CI job that type-checks before any build

Check

TypeScript cannot find useRoute in Nuxt until the auto-import declarations exist. Nuxt stores those generated types in the .nuxt directory and only writes them when the dev server or a build runs, or when you run nuxt prepare. A checkout that has never run any of these has nothing to read.

Check
ls -la .nuxt 2>/dev/null || echo "no .nuxt directory"

Fix

Generate the types and reload the editor window so its language service picks up the new files. In CI, run the same command before nuxt typecheck.

Fix
npx nuxt prepare
npx nuxt typecheck

Verify

nuxt typecheck finishes without TS2304, and the editor shows the type of useRoute() on hover.

Verify
npx nuxt typecheck

Confirmed by nuxt.com

Did this fix it?

Cause 2 of 3

The root tsconfig.json no longer points at the generated configs

You seeThe .nuxt/tsconfig files exist, but tsconfig.json at the project root has its own compilerOptions and no references or extends entry for .nuxt

Check

The generated files only help if the root tsconfig.json loads them. A file replaced by a generic Vue or Vite template, or one with a hand-written paths block, hides the auto-import declarations and the aliases such as #imports. The Nuxt docs advise against editing this file and point to nuxt.config.ts for changes.

Check
grep -n -E "references|extends|paths" tsconfig.json

Fix

Put back the root tsconfig.json from the Nuxt docs for your major version, and move any custom aliases into the alias option of nuxt.config so Nuxt adds them to the generated config.

Fix
// Nuxt 4: tsconfig.json
{
  "files": [],
  "references": [
    { "path": "./.nuxt/tsconfig.app.json" },
    { "path": "./.nuxt/tsconfig.server.json" },
    { "path": "./.nuxt/tsconfig.shared.json" },
    { "path": "./.nuxt/tsconfig.node.json" }
  ]
}

// Nuxt 3: tsconfig.json
{ "extends": "./.nuxt/tsconfig.json" }

Verify

After npx nuxt prepare and an editor reload, useRoute resolves in components and #imports resolves in plain .ts files.

Verify
npx nuxt prepare && npx nuxt typecheck

Confirmed by nuxt.com

Did this fix it?

Cause 3 of 3

Auto-imports are turned off in nuxt.config

You seenuxt.config.ts has imports: { autoImport: false }, and other composables such as ref or useFetch fail in the same way

Check

With imports.autoImport set to false, Nuxt stops auto-importing composables and utilities, useRoute included, so each file must import what it uses. The scan: false option is narrower and only stops auto-imports of your own composables, not the framework ones.

Check
grep -n -E "autoImport|scan" nuxt.config.ts

Fix

Import useRoute explicitly from #imports, which Nuxt keeps available even with auto-imports off, or remove the autoImport line. Prefer #imports over vue-router: Nuxt's useRoute wraps the vue-router one and updates the route only after the new page content has rendered.

Fix
<script setup lang="ts">
import { useRoute } from '#imports'

const route = useRoute()
</script>

Verify

The page shows route.params values and nuxt typecheck no longer reports the name.

Verify
npx nuxt typecheck

Confirmed by nuxt.com

Did this fix it?

How do I stop it coming back?

  • Add nuxt prepare as the first step of every CI job that runs nuxt typecheck or vue-tsc.
  • Keep tsconfig.json exactly as the Nuxt docs show it and put compiler changes under typescript in nuxt.config.ts.
  • If a team turns auto-imports off, add a lint rule or code review note that every composable comes from #imports.

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.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.

Next step

Want this handled for good?

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