nextjs - ✅(Solved) Fix Typing error in .next/dev/types/validator.ts [3 pull requests, 8 comments, 4 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#90332Fetched 2026-04-08 00:20:23
View on GitHub
Comments
8
Participants
4
Timeline
16
Reactions
0
Author
Timeline (top)
commented ×8cross-referenced ×2labeled ×2closed ×1

Error Message

Typescript gives this error: .next/dev/types/validator.ts:5:79 - error TS2307: Cannot find module './routes.js' or its corresponding type declarations.

Fix Action

Fixed

PR fix notes

PR #90354: fix(typegen): use routes.d.ts instead of routes.js in validator import

Description (problem / solution / changelog)

Summary

The generated .next/dev/types/validator.ts was importing from "./routes.js" which does not exist at runtime. The actual generated file is routes.d.ts (a TypeScript declaration file), so tsc was failing with:

.next/dev/types/validator.ts:5:79 - error TS2307: Cannot find module './routes.js' or its corresponding type declarations.

Changes

  • In packages/next/src/server/lib/router-utils/typegen.ts, changed both occurrences of "./routes.js" to "./routes.d.ts" so the generated validator imports from the correct TypeScript declaration file.

Fixes #90332

<!-- Related: previously attempted by #90089 (closed unmerged) -->

Changed files

  • packages/next/src/build/index.ts (modified, +11/-7)
  • packages/next/src/cli/next-typegen.ts (modified, +13/-7)
  • packages/next/src/server/lib/router-utils/route-types-utils.ts (modified, +13/-3)
  • packages/next/src/server/lib/router-utils/setup-dev-bundler.ts (modified, +13/-7)
  • packages/next/src/server/lib/router-utils/typegen.ts (modified, +6/-4)

PR #90384: Revert "keep next-env.d.s unchanged between dev and build"

Description (problem / solution / changelog)

Reverts https://github.com/vercel/next.js/pull/88428 Fixes https://github.com/vercel/next.js/issues/90332 Fixes https://linear.app/vercel/issue/NAR-797/

The generated types were incomplete/inconsistent leading to issues when tsc was ran after next dev. I added a regression test for this scenario.

The recommendation is still to put next-env.d.ts into .gitignore which is what create-next-app already does. Ideally we'd be able to have a stable next-env.d.ts but that's obviously not trivial given the two failed attempts. Or even put next-env.d.ts into .next i.e. out of the top-level folder. However, it's too easy to accidentally ignore .next/next-env.d.ts if that's in include while also having .next in exclude (which we have seen happen in internal apps).

Changed files

  • packages/next/src/build/index.ts (modified, +1/-22)
  • packages/next/src/build/type-check.ts (modified, +3/-3)
  • packages/next/src/cli/next-test.ts (modified, +1/-1)
  • packages/next/src/cli/next-typegen.ts (modified, +2/-19)
  • packages/next/src/lib/typescript/writeAppTypeDeclarations.ts (modified, +27/-8)
  • packages/next/src/lib/verify-typescript-setup.ts (modified, +6/-5)
  • packages/next/src/server/lib/router-utils/route-types-utils.ts (modified, +0/-48)
  • packages/next/src/server/lib/router-utils/setup-dev-bundler.ts (modified, +3/-31)
  • test/development/app-dir/isolated-dev-build-strict-route-types/app/layout.tsx (removed, +0/-11)
  • test/development/app-dir/isolated-dev-build-strict-route-types/app/page.tsx (removed, +0/-3)
  • test/development/app-dir/isolated-dev-build-strict-route-types/isolated-dev-build-strict-route-types.test.ts (removed, +0/-52)
  • test/development/app-dir/isolated-dev-build-strict-route-types/next.config.js (removed, +0/-10)
  • test/development/app-dir/isolated-dev-build/isolated-dev-build.test.ts (modified, +0/-29)
  • test/e2e/app-dir/typed-routes-validator/tsconfig.json (added, +25/-0)
  • test/e2e/app-dir/typed-routes-validator/typed-routes-validator.test.ts (modified, +34/-3)
  • test/e2e/app-dir/typed-routes/tsconfig.json (added, +25/-0)
  • test/e2e/app-dir/typed-routes/typed-routes.test.ts (modified, +22/-4)
  • test/integration/custom-server-types/next-env.d.ts (modified, +1/-1)
  • test/integration/typescript-app-type-declarations/next-env.d.ts (modified, +1/-1)
  • test/integration/typescript-app-type-declarations/next-env.strictRouteTypes.d.ts (modified, +3/-1)
  • test/production/app-dir/types-gen-nounuselocal/types-gen-nounuselocal.test.ts (modified, +1/-1)
  • test/unit/write-app-declarations.test.ts (modified, +11/-139)

Code Example

.next/dev/types/validator.ts:5:79 - error TS2307: Cannot find module './routes.js' or its corresponding type declarations.

5 import type { AppRoutes, LayoutRoutes, ParamMap, AppRouteHandlerRoutes } from "./routes.js"
                                                                                ~~~~~~~~~~~~~

---

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
  Available memory (MB): 65421
  Available CPU cores: 4
Binaries:
  Node: 24.11.1
  npm: 8.19.4
  Yarn: 4.12.0
  pnpm: N/A
Relevant Packages:
  next: 16.2.0-canary.57
  eslint-config-next: 16.2.0-canary.57
  react: 19.2.4
  react-dom: 19.2.4
  typescript: 5.9.3
Next.js Config:
  output: standalone
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://codesandbox.io/p/devbox/divine-violet-3csqv3?workspaceId=ws_MAQtd12sknNpiwtyhwf4Gi

To Reproduce

Run yarn tsc or npx tsc.

Current vs. Expected behavior

Typescript gives this error:

.next/dev/types/validator.ts:5:79 - error TS2307: Cannot find module './routes.js' or its corresponding type declarations.

5 import type { AppRoutes, LayoutRoutes, ParamMap, AppRouteHandlerRoutes } from "./routes.js"
                                                                                ~~~~~~~~~~~~~

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
  Available memory (MB): 65421
  Available CPU cores: 4
Binaries:
  Node: 24.11.1
  npm: 8.19.4
  Yarn: 4.12.0
  pnpm: N/A
Relevant Packages:
  next: 16.2.0-canary.57
  eslint-config-next: 16.2.0-canary.57
  react: 19.2.4
  react-dom: 19.2.4
  typescript: 5.9.3
Next.js Config:
  output: standalone

Which area(s) are affected? (Select all that apply)

TypeScript

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

It seems like the import inside the generated file .next/dev/types/validator.ts should be ./route-types.d.ts, not ./routes.js.

I found this PR #90089, but for some reason it is closed.

extent analysis

Fix Plan

Update tsconfig.json to exclude generated files

  1. Open tsconfig.json and add the following line to the include array:

"exclude": ["next/**/*"]

   This will exclude all files in the `next` directory from being compiled.

2. Update the `compilerOptions` to include the following line:
   ```json
"moduleResolution": "node"

This will allow TypeScript to resolve modules using the Node.js resolver.

  1. Add the following line to the compilerOptions to specify the type declaration file:

"typeRoots": ["node_modules/@types"], "types": ["next"]

   This will tell TypeScript to look for type declarations in the `@types` directory.

#### Update `next.config.js` to use the correct type declaration file

1. Open `next.config.js` and update the `target` option to use the `esnext` target:
   ```javascript
module.exports = {
  // ...
  target: 'esnext',
  // ...
};

This will allow Next.js to generate the correct type declaration file.

  1. Update the build option to use the tsconfig.json file:

module.exports = { // ... build: { tsconfig: 'tsconfig.json', }, // ... };

   This will tell Next.js to use the `tsconfig.json` file for compilation.

#### Verify the fix

1. Run `yarn tsc` or `npx tsc` to compile the TypeScript files.
2. Check that the error is resolved and the `validator.ts` file is generated correctly.

### Extra Tips

* Make sure to update the `tsconfig.json` file to exclude any other generated files that may be causing issues.
* If you're using a custom `tsconfig.json` file for your

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING