出现错误:moment.duration 不是函数

Sum*_*kar 6 node.js npm

我正在尝试以HH:MM:SS格式转换秒。我收到错误 moment.duration 不是函数。

var moment = require("moment-duration-format");

moment.duration(123, "seconds").format("hh:mm:ss");

hel*_*elb 12

moment-duration-format插件取决于 moment,因此您应该先导入/要求它:

var moment = require("moment")
require("moment-duration-format");

moment.duration(123, "seconds").format("hh:mm:ss");
Run Code Online (Sandbox Code Playgroud)


Ami*_*ash 8

我都尝试过

import 'moment-duration-format';

require("moment-duration-format");

但它并没有解决我的问题,而且我仍然得到了TypeError: moment.duration(...).format is not a function所以我看了一下https://www.npmjs.com/package/moment-duration-format以找到如何正确设置它,我发现我有调用它并传递时间以使其正常工作。

import momentDurationFormatSetup from 'moment-duration-format';
momentDurationFormatSetup(moment);
Run Code Online (Sandbox Code Playgroud)