nextra icon indicating copy to clipboard operation
nextra copied to clipboard

nextra: `exports` does not export types for `.`

Open jorins opened this issue 2 years ago • 1 comments

In nextra's package.json, the export field for . does not expose the related type definitions. This causes TypeScript issues for certain values of moduleResolution, e.g. "moduleResolution": "bundler":

error TS7016: Could not find a declaration file for module 'nextra'. 
'/home/jorin/myProject/node_modules/.pnpm/[email protected][email protected]_react-dom@18
[email protected]/node_modules/nextra/dist/index.js' implicitly has an 'any' 
type. There are types at 
'/home/jorin/myProject/node_modules/nextra/dist/types.d.mts', but this result 
could not be resolved when respecting package.json "exports". The 'nextra' 
library may need to update its package.json or typings.%                                                                                  

Setting "moduleResolution": "node16" (which also requires setting "module": "node16") appears to give the same error. Setting "moduleResolution": "node10" seems to remedy this, but node10 is a a bit of a legacy choice and appears to be causing headaches elsewhere for me. Although node10 is the value set by next when no other value is present in tsconfig.json, bundler is the chosen default of @tsconfig/next and I'm thinking it's the value that makes the most sense for any modern static site.

Applying the following patch using pnpm patch seems to solve the issue properly:

diff --git a/package.json b/package.json
index 8ceebb9bc9f09bef59ef667871f1552337be979f..1e836e83bec157fc7b90aeba67a74930b8061980 100644
--- a/package.json
+++ b/package.json
@@ -10,7 +10,10 @@
   "main": "./dist/index.js",
   "exports": {
     "./package.json": "./package.json",
-    ".": "./dist/index.js",
+    ".": {
+      "import": "./dist/index.js",
+      "types": "./dist/types.d.ts"
+    },
     "./catch-all": "./dist/catch-all.js",
     "./data": {
       "import": "./dist/ssg.js",

I'd wager this is the patch you'll want to put upstream too, but I'm not sure if perhaps something similar needs to be done with ./loader and ./catch-all and my understanding of JS module shenanigans is not quite comprehensive enough for me to confidently publish a PR.

During my research I found this tool: Are The Types Wrong? Perhaps you'll find it helpful in your endeavours of addressing this issue.

Thanks for your lovely work, Jorin

jorins avatar Dec 26 '23 21:12 jorins

I think Nextra v3 will fix that. In my opinion, You may use "now moduleResolution": "node" for now.

jellli avatar Dec 27 '23 12:12 jellli