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

Property 'tz' does not exist on type 'Moment' Error #906

Closed
rama41222 opened this issue Nov 5, 2020 · 15 comments
Closed

Property 'tz' does not exist on type 'Moment' Error #906

rama41222 opened this issue Nov 5, 2020 · 15 comments
Labels

Comments

@rama41222
Copy link

rama41222 commented Nov 5, 2020

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

@sliceofcode
Copy link

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'.

@rama41222
Copy link
Author

Any update regarding this ?

@sliceofcode
Copy link

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;

@ellenaua ellenaua added the bug label Nov 14, 2020
@jrmyio
Copy link

jrmyio commented Apr 23, 2021

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.

@manuel-di-iorio
Copy link

No issues after installing @types/moment-timezone@0.5.30
Thanks @ConneXNL

@rama41222
Copy link
Author

I'm closing this since the issue is resolved.

@vishal21-onelife
Copy link

vishal21-onelife commented Nov 17, 2021

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

@AbdelrahmanHafez
Copy link

AbdelrahmanHafez commented Apr 18, 2022

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';
`

@masimpson123
Copy link

I was having this problem in just my test environment. I had to add moment-timezone to the types array in tsconfig.spec.ts. If no types array is present, all @types packages are visible. If a types array is present, only the listed packages are used by the compiler.

TSConfig and Types array documentation:
https://www.typescriptlang.org/tsconfig#types

Refactoring the app to use moment-timezone rather than moment also fixed the problem, but I decided that the tsconfig solution was simpler and less risky.

@MonstraG
Copy link

In my case, had to downgrade to 2.29.1 (2.29.2 still failed to build), resulting in

		"moment": "2.29.1",
		"moment-locales-webpack-plugin": "^1.2.0",
		"moment-timezone": "^0.5.34",

my failing usecase:

import moment, { Moment } from "moment";
import "moment-timezone";

const func1 = (day: Moment, seconds: number, timeZone: string): Moment =>
	day.clone().add(seconds, "seconds").tz(timeZone, true).local();

@aailyin
Copy link

aailyin commented Apr 27, 2022

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.

@DiRover
Copy link

DiRover commented May 20, 2022

I'm having the same issue.

moment version: 2.24.3
moment-timezone version: 0.5.34

When I wrote the next code, the issue is gone:

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

@gilmoreorless
Copy link
Member

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 #982 (comment)

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

@shnigi
Copy link

shnigi commented Jun 3, 2022

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

dimaslanjaka added a commit to dimaslanjaka/static-blog-generator that referenced this issue Jan 9, 2023
@sahilSOLT
Copy link

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

//then---------

var currentTimEzone = moment.tz.guess();
console.log('timezone here',currentTimEzone)

dimaslanjaka added a commit to dimaslanjaka/static-blog-generator that referenced this issue Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests