moment-timezone: How to resolve the 'tz' build error.
Error below :
ERROR in [default] /home/myubuntu/work/angular_cli_webpack/src/app/app.log.component.ts:264:31
Property 'tz' does not exist on type 'Moment'.
[environment]
angular2: 2.0.0
angular-cli webpack : 1.0.0-beta.14
moment": "^2.14.1
moment-timezone": "^0.5.5
[angular-cli.json]
...
......
"scripts": [
"../node_modules/moment/moment.js",
"../node_modules/moment-timezone/builds/moment-timezone-with-data-2010-2020.min.js",
"../node_modules/locomote-video-player/dist/locomote.min.js"
],
......
...
[app.log.component.ts]
import * as moment from 'moment';
import 'moment-timezone';
...
...
class AppLogComponent {
constructor() {}
public func() {
var utc_time = moment.utc(unix_tm_sec*1000);
var camera_time = utc_time.tz(timezone); <--- error position.
}
}
...
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 8
- Comments: 17 (1 by maintainers)
@MingyuJeon Installing
@types/moment-timezone
should solve this issue:npm i @types/moment-timezone --save-dev
With 2018 typescript use:
npm install @types/moment-timezone -S
import * as moment from 'moment';
import 'moment-timezone';
I somehow got it to work as this:
npm install moment moment-timezone --save --global
Then in the relevant file:
since we are both using angular, this is how I did it.
npm install moment-timezone -S
import moment from "moment-timezone";
the
@types/moment-timezone
not works for me 😦Same issue here.
Property 'tz' does not exist on type 'typeof moment'.
It looks like the
@types/moment-timezone
package has been deprecated according to #858I fixed it by uninstalling that package and using this code (from above):
The solution is: Add these in the Package.json “@types/moment”: “^2.13.0”, “@types/moment-timezone”: “^0.5.30”, “moment”: “^2.27.0”, “moment-timezone”: “^0.5.31”,
Then in the relevant file:
import moment from “moment”; import “moment-timezone”;
After this you can use moment and tz functions.
I could not import moment-timezone@0.5.14 with moment@2.20.1. They were apparently incompatible.
However, changing moment to version 2.18.1 fixed it for me.
I used this import syntax:
May help somebody: Faced issue using local own npm package, which
had
dependency on@types/moment-timezone
, but no imports was done withfrom "moment-timezone"
, but package consumer - had So, as a result type casting failed since dependency usedmoment
(because of no imports), but package consumer -moment-timezone