Search code examples
typescriptflatpickr

TypeScript can't find module flatpickr, even though it includes typescript types


I'm trying to import a module that contains TypeScript definitions. Installing @types/flatpickr even warns, that one doesn't need to install it.

It seems like tsc doesn't pick up the *.d.ts files of the installed package and then simply ignores the whole package.

I created a simple JS project that shows the issue: https://github.com/Strayer/typescript-import-flatpickr-issue

The error message reported by tsc is:

test.ts:1:23 - error TS2307: Cannot find module 'flatpickr'.

1 import flatpickr from "flatpickr";

                        ~~~~~~~~~~~

The types themselves can be found in node_modules/flatpickr/dist/types after installing the dependencies.

What do I need to do to help TypeScript find the included type definitions?


Solution

  • Your tsconfig.json uses "module": "es6". According to the compiler options definition, this causes the compiler to lookup modules in a classic manner. The classic lookup works for some scenarios but does not support node modules defined via packages.json, which is the case for flatpickr.

    What you need to change for this to work for you is either set the tsconfig.json to "moduleResolution": "node" or perhaps use "module": "commonjs" which will switch both the emitted modules and resolution mechanism to node.js standards.

    Either option will cause your TypeScript to compile as it can now find the module type definitions.

    It is however possible that your project will now fail to load the module in run-time as TypeScript does not handle that. If this is the case and you cannot find a solution, I suggest opening a new question with additional information about the run-time.