121

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'); 
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
Sanath
  • 4,774
  • 10
  • 51
  • 81

9 Answers9

218

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.

Community
  • 1
  • 1
Matt Goodrich
  • 4,875
  • 5
  • 25
  • 38
  • 8
    Any ideas why this is working on my local machine using moment (for years) and today it's throwing the error today? Using just 'moment' is also working locally on my machine but not working on colleagues machine. For example, this is works: const moment = require('moment'); var date = moment(new Date()).tz('America/New_York').format('ddd. MMMM Do YYYY, h:mm a'); (this works for me) – Matt Kim Feb 13 '18 at 18:19
  • 1
    I don't fully understand your first question. Could you please reword? You can replace `moment(new Date())` with just `moment()`. I edited my response with the npm install commands, so please try them again on any environmen (your local machine, colleague's machine, production machines). – Matt Goodrich Feb 13 '18 at 18:29
  • 2
    Just broke on my machine too. Just wondered why it didn't before. – Kevin Amiranoff Dec 10 '18 at 11:03
  • 1
    @MattKim I have the same issue. When I run `npm i`, it installs my `moment@2.24.0`. I don't knw why but npm is installing `moment-timezone` with a sub repository `node_modules` and `moment@2.24.0`, so the moment lib I use is not updated. In production, I don't have this sub node_module dir with the wrong moment. Why is `npm` doing this on my computer everytime? I use same npm version on both machines. – Patrick Portal Apr 11 '19 at 21:25
  • 4
    @MattKim Finally it was an issue with the `package-lock.json`, delete it / `npm i` and it fixes the issue :/ – Patrick Portal Apr 11 '19 at 21:55
  • 3
    I just spent way too much time trying to fix the test suite. Thanks so much for this answer! – Ariel Salem Jul 12 '19 at 23:14
58

Below code for me...

import moment from 'moment';
import 'moment-timezone';
barbsan
  • 3,418
  • 11
  • 21
  • 28
Purushottam Sadh
  • 1,087
  • 10
  • 5
9

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();
Gadani
  • 101
  • 2
  • 6
9

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'),

David Dehghan
  • 22,159
  • 10
  • 107
  • 95
8

Error -> TypeError: (0 , moment_timezone_1.default) is not a function

This is typescript error, by default It's take moment as function

I fix this issue, by replacing

import moment from 'moment-timezone'

import * as moment from 'moment-timezone'

moment().tz("America/Los_Angeles").format()

Piyush Jindal
  • 81
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 18 '22 at 09:55
5

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

In my case, moment-timezone uses moment@2.24.0, and moment itself version is 2.18.1. Causes moment-timezone decorated wrong version of moment.

I've change yarn.lock like this:

moment-timezone@0.5.13:
  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"

moment@2.18.1, 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.

Xu Tongbin
  • 67
  • 1
  • 1
2

Use: moment-timezone

const moment = require('moment-timezone');
const time = moment.tz(1412144245453, 'America/Los_Angeles').format('MM/DD/YYYY h:mm a');
console.log("time : ", time);

Output: time : 09/30/2014 11:17 pm

Sahil Thummar
  • 1,926
  • 16
  • 16
2

Very old thread, but since I also ran into this issue and just as others have pointed out it was working and then stopped working, I tried digging into why it was happening like this?

So I had the following require statement in foo.js

const moment = require('moment');
moment.tz.setDefault('Europe/Berlin');

I ran my project and it worked totally fine, however one of my test files bar.test.js required something from foo.js and when I ran mocha bar.test.js I was presented with TypeError: Cannot read property 'setDefault' of undefined

The reason it worked in foo.js was because somewhere in my project I had done the following

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

and moment-timezone has a dependency on moment. So it loads the moment object first and then adds the additional timezone functionality as sort of a plugin. Then, when I require moment in my foo.js file it picks the object from the cache which has the tz functionality in it. But when I run my test file only, this tz functionality is not added on the moment object and hence it fails

So it is necessary to have require('moment-timezone') instead of require('moment')

I hope this helps

aekant
  • 199
  • 1
  • 5
-2

Moment should be a function call. So use let myDate = moment().tz(...)

See https://momentjs.com/timezone/docs/ for more details.

EDIT

You should also ensure that you are including the timezone extension to the moment library either through the correct npm install and require (for Node) or the correct script tags (for general browser usage). See the linked documents for the libraries/scripts to include.

SECOND EDIT

Should anyone have scrolled this far on a question with a +120 accepted answer:

The suggestion that moment was a function and requires brackets refers to the un-edited version of the posted question which had the syntax

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

John
  • 1,313
  • 9
  • 21