next.js: [NEXT-779] next/* - Typescript cannot find module when moduleResolution=nodenext and type=module
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"
topackage.json
- change
moduleResolution
tonodenext
- 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 bynpx create-turbo@latest
, but we want sources to be undersrc
and not the root folder.
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 43
- Comments: 16 (6 by maintainers)
Commits related to this issue
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 5 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 5 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 5 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 5 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 5 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 5 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 5 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 5 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 5 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 5 months ago
- DeletePost is ready! — committed to dmrado/next14correct by deleted user 5 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 4 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 4 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 4 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 4 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 4 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 4 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 4 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 4 months ago
- Allow ts_project to see package.json (for esmodule resolution). https://github.com/vercel/next.js/issues/46078 — committed to zemn-me/monorepo by Zemnmez 4 months ago
Note that
nodenext
andbundler
are fundamentally different module resolution algorithms.– https://stackoverflow.com/a/71473145/368691
nodenext
needs to be supported by Next.js. Otherwise this causes major maintenance headache for organizations with large monorepos.I’ve been using these imports with
.js
suffixes (eg.next/image.js
,next/server.js
,next/link.js
, etc), and it’s worked so far.However, one big footgun with this which I just ran into (pretty hard to debug) is if you import
next/link.js
, the feature of TypeScript type checking onLink[href]
usingtypedRoutes: true
by @shuding just fails silently:The offending line in the
.next/types/link.d.ts
file looks like this:FYI, this went away for me when I changed these settings in my
tsconfig.json
:module
:nodenext
->esnext
moduleResolution
:nodenext
->bundler
(Read more here: https://www.totaltypescript.com/tsconfig-cheat-sheet)
Submitted a pull request.
Until that is merged/released, here is a local patch that you can use:
EDIT: sorry, just realized this is similar to this comment
In
nodenext
we have:The workaround is:
However, this does not work (still needs
Link.default
):Adding
to
next/link.d.ts
works:I guess the wildcard export is not working:
As an alternative to @lucgagan suggestion above, adding wildcard exports to
package.json
works for me:Node’s support for subpath patterns is documented here.