marnusw / date-fns-tz

Complementary library for date-fns v2 adding IANA time zone support
MIT License
1.07k stars 116 forks source link

Attempted import error: Cannot find module 'date-fns-tz/format' or its corresponding type declarations #294

Open Bielousov opened 3 months ago

Bielousov commented 3 months ago

Potentially related issue: https://github.com/marnusw/date-fns-tz/issues/291

    "date-fns": "3.6.0",
    "date-fns-tz": "3.1.3",

Node: 18.20 (also tried 20.16) TypeScript: 5.4.4 tsconfig:

  "compilerOptions": {
    "strict": true,
    "skipLibCheck": true,
    "moduleResolution": "node"
  },
  "exclude": ["node_modules"]

Upgrading from v1.3.7

When attempting to import any exported submodule directly, e.g:

import { format } from 'date-fns-tz/format';

TS cannot resolve the path, even though it is listed in the date-fns-tz/package.json:

Cannot find module 'date-fns-tz/format' or its corresponding type declarations. There are types at './node_modules/date-fns-tz/dist/esm/format/index.d.ts', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'.ts(2307)

Changing this to import { format } from 'date-fns-tz'; works, but it increases the bundle size significantly.

Consider exporting type declarations: https://stackoverflow.com/a/77566206 along with ems and commonjs modules.

Bielousov commented 3 months ago

It appears that changing "moduleResolution" to nodenext, node16 or bundler fixes the import issue, although this isn't an option for me unfortunately. Also this wasn't the case in date-fns-tz@v1.3.7, nor it is mentioned in documentation anywhere, nor used in the library own tsconfig.