1. ホーム
  2. javascript

[解決済み] TypeError: moment().tz は関数ではありません。

2023-01-21 04:13:01

質問

jasmineを使ったテストでは、このエラーが発生します。

TypeError: moment.tz is not a function.

私がテストしようとする私のコードは

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

どのように解決するのですか?

修正

Node.jsを使用している場合、誤って

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

ではなく

const moment = require('moment-timezone'); // モーメントタイムゾーン

また、moment-timezoneがインストールされていることを確認し

npm install moment-timezone --save

説明

タイムゾーンがない状態でmomentをインストールすると、バグが発生する可能性があります。 require('moment') でインストールし、後で npm install moment-timezone を更新し、その後 require .