Skip to content

Type definition cannot be resolved under moduleResolution: "NodeNext" #248

@Jack-Works

Description

@Jack-Works

package.json

{
    "type": "module",
    "dependencies": {
        "typescript": "4.9",
        "urlcat": "^3.1.0"
    }
}

tsconfig.json

{
    "compilerOptions": {
        "module": "NodeNext",
        "moduleResolution": "nodenext",
        "strict": true
    },
    "include": ["./src"]
}

src/index.ts

import urlcat from 'urlcat'
//                 ~~~~~~~~
urlcat()

Reports the following error:

Could not find a declaration file for module 'urlcat'. 'node_modules/urlcat/dist/index.mjs' implicitly has an 'any' type.
  Try `npm i --save-dev @types/urlcat` if it exists or add a new declaration (.d.ts) file containing `declare module 'urlcat';`ts(7016)

Suggested changes to fix this problem

diff --git a/package.json b/package.json
--- a/package.json
+++ b/package.json
@@ -11,8 +11,14 @@
   "types": "dist/index.d.ts",
   "exports": {
     ".": {
-      "import": "./dist/index.mjs",
-      "require": "./dist/index.js"
+      "import": {
+        "types": "./dist/index.d.mts",
+        "default": "./dist/index.mjs"
+      },
+      "require": {
+        "types": "./dist/index.d.ts",
+        "default": "./dist/index.js"
+      }
     },
     "./package.json": "./package.json"
   },

and copy ./dist/index.d.ts to ./dist/index.d.mts

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions