TypeError: moment().tz is not a function

88,106

Solution 1

Fix

If you're using Node.js, you may accidentally be using

const moment = require('moment'); //moment

instead of

const moment = require('moment-timezone'); //moment-timezone

Also, make sure you have installed moment-timezone with

npm install moment-timezone --save

Explanation

The bug of requiring moment without timezones could occur by installing moment with require('moment'), later deciding to npm install moment-timezone, and then forgetting to update the require.

Solution 2

Below code for me...

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

Solution 3

For Node.js, According to the original documentation: moment js documentation

You should do

npm install moment-timezone

Then use it like this

var moment = require('moment-timezone');
moment().tz("America/Los_Angeles").format();

Solution 4

for Typescript: Works as of April 2021

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

cont x = moment.tz('America/Los_Angeles').format('YYYY-MM-DD HH:mm:ss ZZ');

cont y = moment().isBetween(
            moment.tz('1-1-2021', 'America/Los_Angeles'),
            moment.tz('1-1-2021', 'America/Los_Angeles').add(2, 'hours'),

Solution 5

I've encountered this problem too. It works for years, but after a refactor, it doesn't work. As I've investigated, [email protected] depends on moment@>=2.9.0, which might be different from moment itself.

In my case, moment-timezone uses [email protected], and moment itself version is 2.18.1. Causes moment-timezone decorated wrong version of moment.

I've change yarn.lock like this:

[email protected]:
  version "0.5.13"
  resolved "https://arti-dev.ss.aws.fwmrm.net/api/npm/fw-npm/moment-timezone/-/moment-timezone-0.5.13.tgz#99ce5c7d827262eb0f1f702044177f60745d7b90"
  integrity sha1-mc5cfYJyYusPH3AgRBd/YHRde5A=
  dependencies:
    moment ">= 2.9.0"

[email protected], moment@>= 2.9.0:
  version "2.18.1"
  resolved "https://arti-dev.ss.aws.fwmrm.net/api/npm/fw-npm/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f"
  integrity sha1-w2GT3Tzhwu7SrbfIAtu8d6gbHA8=

moment & moment-timezone could be used substitute for each other in this case.

Share:
88,106
Sanath
Author by

Sanath

Senior Frontend Engineer with 12+ years of experience in the software development industry including 7+ years of Australian experience, enabling me to improve my professional skills and gain technical competencies. I have worked extensively with Angular JS, React, VueJs, and the related front-end technology stack. My experience in NodeJs and J2EE based technologies has allowed me to gain a good technical understanding about the total application architecture and the best practices required in end to end implementation of a business solution. I am passionate about the evolving front-end technology landscape and have been working with some of the cutting-edge technologies in that domain.

Updated on February 15, 2022

Comments

  • Sanath
    Sanath over 2 years

    When testing using jasmine, I am getting this error.

    TypeError: moment.tz is not a function

    My code that I try to test is

    let myDate = moment().tz(undefined, vm.timeZone).format('YYYY-MM-DD');