vercel / vercel/next.js

[NEXT-779] next/* - Typescript cannot find module when moduleResolution=nodenext and type=module

Open
#46,078 26 comments 109 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

TypeScript
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Verify canary release
  • I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.6.0: Thu Sep 29 20:13:56 PDT 2022; root:xnu-8020.240.7~1/RELEASE_ARM64_T6000
Binaries:
  Node: 16.17.0
  npm: 8.15.0
  Yarn: N/A
  pnpm: 7.25.0
Relevant packages:
  next: 13.1.7-canary.18
  eslint-config-next: 13.0.0
  react: 18.2.0
  react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)

TypeScript

To Reproduce
  • pnpm create next-app (with typescript)
  • add "type": "module" to package.json
  • change moduleResolution to nodenext
  • run npx tsc --noEmit
Describe the Bug

Typescript complain about next/*, for example:

Cannot find module 'next/head' or its corresponding type declarations.

I believe this is because there is no exports field in node_modules/next/package.json.

Expected Behavior

Typescript shouldn't complain.

Context

  • We use "type": "module" as we want repo scripts to be esm rather than cjs.
  • We use "moduleResolution": "nodenext" as we use a setup similar to the monorepo created by npx create-turbo@latest, but we want sources to be under src and not the root folder.
Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-779

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the problem with a TypeScript Next.js app created by pnpm create next-app, adding type: module, setting moduleResolution to nodenext, and running npx tsc --noEmit. Inspect node_modules/next/package.json and the app's package configuration; done means TypeScript resolves next/* modules without errors under this setup.

Written by the indexing model from the issue text.

Assessment

Tech stack
next.js, typescript
Domain
build-system, developer-experience
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.