annexare / Countries

Countries, Languages & Continents data (capital and currency, native name, calling codes).
https://annexare.github.io/Countries/
MIT License
1.18k stars 429 forks source link

The type package export is broken for TS projects using moduleResolution node16/nodenext/bundler #125

Closed jraoult closed 1 year ago

jraoult commented 1 year ago

Use case for the feature

When consuming countries-list from a typescript project with moduleResolution: node16/nodenext/bundler in the configuration, the types are not "found" and tsc errors:

error TS7016: Could not find a declaration file for module 'countries-list'. '[...]/node_modules/countries-list/index.min.mjs' implicitly has an 'any' type.

and then

There are types at '[...]/node_modules/countries-list/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'countries-list' library may need to update its package.json or typings.

See this comment for an example of what needs to be done https://github.com/gxmari007/vite-plugin-eslint/pull/60#issuecomment-1416264911, which would result (assuming index.d.mts is bundled as well):

[...]
    ".": {
      "import": {
        "types": "./index.d.mts",
        "default": "./index.min.mjs"
      },
      "require": {
        "types": "./index.d.ts",
        "default": "./index.min.js"
      }
    }
[...]
dmythro commented 1 year ago

Hi, thanks, gonna check this out. Exports should already cover all of that.

StijnMaenhautCZ commented 1 year ago

I assume this is related. Installed v3.0.1 on a Node v16.20.2 instance and getting the following error (using vanilla javascript, no typescript):

This dependency was not found: 
* countries-list in ./node_modules/thread-loader/dist/cjs.js??ref--3-0!./node_modules/
babel-loader/lib??ref--3-1!./node_modules/vue-loader/lib??vue-loader-options!./components
/forms/tp-configurator-contact-form.vue?vue&type=script&lang=js&, ./node_modules/thread-
loader/dist/cjs.js??ref--3-0!./node_modules/babel-loader/lib??ref--3-1!./node_modules/eslint-
loader!./components/forms/tp-contact-form/tp-contact-form.js?vue&type=script&lang=js& and 6 others

As soon as I switch back to v2.6.1, everything works fine.

dmythro commented 1 year ago

@StijnMaenhautCZ how about Node 18 LTS? Node 16 reached its end of life already.

Could you please share easiest way to reproduce the issue? I tried v3 on all of my projects when released and it worked without issues.

Gonna definitely check it out soon.

jraoult commented 1 year ago

@dmythro here's a reproduction of the issue I initially reported: https://stackblitz.com/edit/typescript-nsv8tl?file=index.html. You can see the error Cannot find module 'countries-list' or its corresponding type declarations.(2307) in the index.ts file. Applying the changes I described earlier to packages.json fixes it from my quick tests.

dmythro commented 1 year ago

@jraoult please verify if v3.0.5 works for you now.

jraoult commented 1 year ago

@dmythro seems all good now. Thanks for the quick fix.

dmythro commented 1 year ago

@dmythro seems all good now. Thanks for the quick fix.

Awesome, thanks for reporting/validating :)