4

For the last day I researched a mysterious issue in which a moment-timezone feature would not work under particular, seemingly arbitrary circumstances. I discovered that the runtime version of my moment-timezone library was changing at some point from version 0.5.17 to 0.5.13.

Before adding more details, is this a node.js issue or a moment-timezone issue?

The specific problem with moment-timezone I ended up resolving using yarn selective-version-resolutions, but if this is a actually a node.js issue, I'm thinking more extreme measures are called for (yarn install --flat?).

I don't know which dependency was causing the version to change at runtime, but this was the relevant section from my yarn.lock file before adding the resolutions section:

moment-timezone@0.5.17:
  version "0.5.17"
  resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.17.tgz#3c8fef32051d84c3af174d91dc52977dcb0ad7e5"
  dependencies:
    moment ">= 2.9.0"

moment-timezone@^0.5.0, moment-timezone@^0.5.4, moment-timezone@~0.5.5:
  version "0.5.13"
  resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.13.tgz#99ce5c7d827262eb0f1f702044177f60745d7b90"
  dependencies:
    moment ">= 2.9.0"

As you can see, my direct dependency was on version 0.5.17, but the dependency of my other modules was getting resolved to version 0.5.13. But I don't understand how at some point my dependency was getting resolved to 0.5.13.

To check the moment-timezone version I simply used moment.tz.version. That means that in my production code, the following code printed 0.5.17 until at some point suddenly printing 0.5.13:

const moment = require('moment-timezone');
console.log(`moment.tz.version: ${moment.tz.version}`);

One last detail: the moment-timezone function that was breaking when the version changed to 0.5.13 was the optional flag on the moment.tz function added in version 0.5.14, in this code:

moment(utcDateTime, format).clone().tz(timezone, true)

Can anyone explain how this is possible? I hope it's a moment-timezone bug and not a node.js bug...

4

1 回答 1

0

我今天遇到了同样的问题,几个小时后,我想我知道发生了什么。如果您检查 moment-timezone 代码,您会看到它在初始化时需要 moment,添加tz与时区相关的所有内容的属性,最后返回修改后的 moment 实例:

因此,如果您的直接依赖项和另一个模块中的依赖项解析为相同的moment版本,则相同的对象会被修改两次,而您获得的最终版本仅取决于您所需的顺序moment-timezone和具有moment-timezone依赖项的模块。

事实上,在moment-timezone代码中,你可以看到这些行被注释掉了:

// Do not load moment-timezone a second time.
    // if (moment.tz !== undefined) {
    //  logError('Moment Timezone ' + moment.tz.version + ' was already loaded ' + (moment.tz.dataVersion ? 'with data from ' : 'without any data') + moment.tz.dataVersion);
    //  return moment;
    // }

我不知道为什么开发人员对此进行了注释,但显然这是一种已知行为。

于 2019-11-20T04:22:59.513 回答