Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript cannot find types when using NodeNext or Node16 modules #5028

Closed
adam-coster opened this issue May 22, 2022 · 0 comments · Fixed by #5045
Closed

Typescript cannot find types when using NodeNext or Node16 modules #5028

adam-coster opened this issue May 22, 2022 · 0 comments · Fixed by #5045

Comments

@adam-coster
Copy link
Contributor

adam-coster commented May 22, 2022

Describe the bug

Typescript 4.7+ includes new the new "Node16"/"NodeNext" options for configuration options "module" and "moduleResolution". These adhere strictly to Node's package.json "exports" fields rules -- when that field is set the other root fields (like "types") are ignored.

The package.json for @sveltejs/adapter-static, and presumably other SvelteKit packages, does not list types in its "exports" field, causing Typescript to be unable to find types when using NodeNext module strategies.

It's an easy fix, and Typescript 4.7 is still in beta so the impact of this will slowly grow as adoption of latest Typescript versions increases.

Here's the diff generated via patch-package for @sveltejs/adapter-static:

diff --git a/node_modules/@sveltejs/adapter-static/package.json b/node_modules/@sveltejs/adapter-static/package.json
index c3f39a0..6c0dac8 100644
--- a/node_modules/@sveltejs/adapter-static/package.json
+++ b/node_modules/@sveltejs/adapter-static/package.json
@@ -12,6 +12,7 @@
   "main": "index.js",
   "exports": {
     ".": {
+      "types": "./index.d.ts",
       "import": "./index.js"
     },
     "./package.json": "./package.json"

Reproduction

With Typescript 4.7-beta and "module" set to "ESNext" or "NodeNext", we get an error finding the types:

https://www.typescriptlang.org/play?moduleResolution=99&target=99&jsx=0&allowSyntheticDefaultImports=true&ts=4.7.0-beta#code/JYWwDg9gTgLgBAQwCYLDAplAyjBNgDGcAZlBCHAOQACAzgG7oA2GAVrQPTKoZQC0tXPgKUA3EA

If we change the "module" to "CommonJs", the types are found successfully (ignore the newly-revealed error, it's unrelated to this issue):

https://www.typescriptlang.org/play?target=99&ts=4.7.0-beta&jsx=0&module=1&allowSyntheticDefaultImports=true#code/JYWwDg9gTgLgBAQwCYLDAplAyjBNgDGcAZlBCHAOQACAzgG7oA2GAVrQPTKoZQC0tXPgKUA3EA

Logs

No response

System Info

System:
  OS: Windows 10 10.0.22000
  CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700KF
  Memory: 43.34 GB / 63.82 GB
Binaries:
  Node: 16.14.2 - ~\AppData\Local\Volta\tools\image\node\16.14.2\node.EXE
  npm: 8.10.0 - ~\AppData\Local\Volta\tools\image\npm\8.10.0\bin\npm.CMD
Browsers:
  Edge: Spartan (44.22000.), Chromium (101.0.121)
  Internet Explorer: 11.0.22000.120
npmPackages:
  @sveltejs/adapter-static: ^1.0.0-next.31 => 1.0.0-next.31 
  @sveltejs/kit: ^1.0.0-next.338 => 1.0.0-next.338

Severity

serious, but I can work around it

Additional Information

No response

@benmccann benmccann added this to the 1.0 milestone May 23, 2022
Rich-Harris added a commit that referenced this issue May 24, 2022
Rich-Harris added a commit that referenced this issue May 24, 2022
* add types to pkg.exports - fixes #5028

* types first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants