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

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

Open
Jack-Works opened this issue Feb 24, 2023 · 8 comments · May be fixed by #257
Open

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

Jack-Works opened this issue Feb 24, 2023 · 8 comments · May be fixed by #257

Comments

@Jack-Works
Copy link

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

@balazsbotond
Copy link
Owner

Thank you for taking the time to open this issue and suggest fixes to the problem. I kindly ask that you consider creating a pull request with your suggested changes, so that we can review and discuss them further.

Thank you again for your contribution!

@Jack-Works
Copy link
Author

Thank you for taking the time to open this issue and suggest fixes to the problem. I kindly ask that you consider creating a pull request with your suggested changes, so that we can review and discuss them further.

Hi, my suggestion needs to copy the output file, which requires changing the build step, I don't know if that is good.

@omarkhatibco
Copy link

if you use patch-package you could make it works by adding to package.json

-      "require": "./dist/index.js"
+      "require": "./dist/index.js",
+      "types": "./dist/index.d.ts"

@devnomic
Copy link

Same issue with moduleResolution bundler typescript 5+. There is no type definition.

Could not find a declaration file for module 'urlcat'. '/hello/node_modules/urlcat/dist/index.mjs' implicitly has an 'any' type.

@trevorsmith
Copy link

I've created a PR to fix this issue: #264

@benfoster
Copy link

Any plans to merge the above?

@ethansimmons
Copy link

I'm running to this issue, is there a workaround, or a library that works better with typescript.

@alii
Copy link

alii commented Oct 10, 2024

@ethansimmons while I owe a lot of thanks to urlcat over the years, I've started working on my own version to replace it called pathcat https://www.npmjs.com/package/pathcat

I've also left a note of recognition to urlcat in the readme of pathcat 🙂

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

Successfully merging a pull request may close this issue.

8 participants