moment-timezone: Property 'tz' does not exist on type 'Moment' Error

nodejs version: 12.18.4 moment-timezone version: 0.5.31 moment version: ^2.29.0 ts-node version: happens both in 9.0.0(latest), 8.6.2 command: ts-node --files server/app.ts --project server/tsconfig.build.json

usage

import moment from 'moment-timezone'
moment(somedate).tz(sometimezone)

No @types/moment or @types/moment-timezone installed since deprecated.

What could be the reason ?

PS: Already checked this Issue

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 15 (1 by maintainers)

Commits related to this issue

Most upvoted comments

This issue re-occured again with the following versions:

"moment": "2.29.3",
"moment-timezone": "0.5.34"

Downgrading moment to 2.29.2 fixes the issue.

Edit: Another way to fix things for good is to get rid of moment and rely completely on moment-timezone, you don’t need to use both.

import moment from 'moment-timezone';

// use moment as you would if you had imported 'moment';
`

Using the latest version at this time of writing: moment-timezone@0.5.33 and @types/moment-timezone@0.5.30 I do not have the issue:

import 'moment-timezone';
import moment from 'moment';

moment().tz('CEST') <- no errors…

So I think this is issue is resolved.

I resolved this by downgrading the moment-timezone package to 0.5.28 (instead of 0.5.31).

I found that the offending lines in 0.5.31 with my setup are:

index.d.ts:

zonesForCountry<T extends true>(country: string, with_offset: T): T extends true ? MomentZoneOffset[] : never;
zonesForCountry<T extends false>(country: string, with_offset?: T): T extends false ? string[] : never;

This is generally caused by having multiple versions of moment imported at the same time, due to different dependency trees. I’ve written a full description at https://github.com/moment/moment-timezone/issues/982#issuecomment-1119540905

As noted by @DiRover, importing from only moment-timezone should fix the issue in most cases.

The same issue with versions:

"moment": "^2.29.3",
"moment-timezone": "^0.5.34",
"@types/moment-timezone": "^0.5.30"

Downgraded to 2.29.1, but it contains vulnerabilities.

Solved by @gilmoreorless comments. Changed my import to import moment, { tz } from "moment-timezone"; And working code const timeOffset = moment().tz(timezone).format("Z")

i sove this problem

  1. install package npm i moment-timezone

2.import time-zone in app.component.ts file import ‘moment-timezone’;

3.save and run

-check some moment-timezone package version not work

I’m having exactly the same issue.

node version: 12.14.1 moment version: 2.24.0 moment-timezone version: 0.5.31 ts-node version: 8.3.0

Usages:

import * as moment from 'moment';
import 'moment-timezone';
moment.tz.setDefault('UTC');
import * as moment from 'moment-timezone';
moment.tz.setDefault('UTC');

They yield same result:

TS2339: Property 'tz' does not exist on type 'typeof moment'.