【问题标题】:Using moment-timezone with webpack在 webpack 中使用时刻时区
【发布时间】:2016-01-21 12:42:58
【问题描述】:

我有在webpack 上运行的react/redux 应用程序,并且想使用moment-timezone 库。我已经在 SO 中检查了How should I use moment-timezone with webpack? 问题,并按照问题中的描述安装了json-loader。但仍然在我的应用程序中要求moment-timezone 时:

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

它引发了一个错误:

ERROR in ./~/moment-timezone/index.js
Module not found: Error: Cannot resolve 'file' or 'directory' /path-to-the-project/node_modules/moment-timezone
 @ ./~/moment-timezone/index.js 2:15-51

@ ./~/moment-timezone/index.js 2:15-51 在哪里:

moment.tz.load(require('./data/packed/latest.json'));

但是,当我从构建文件夹中包含缩小版本时,它可以正常工作,如下所示:

const momentTz = require('moment-timezone/builds/moment-timezone-with-data.min');

更新:

webpack 配置:

let jsonLoader = require('json-loader');
loaders: [
  {
    include: /\.json$/,
    loaders: [jsonLoader]
  }
]

【问题讨论】:

  • 那么有没有办法添加 moment-timezone 以与 moment 正确互操作,以便您可以使用 moment.tz()?

标签: node.js timezone momentjs webpack


【解决方案1】:

我的问题是在 webpack 配置中。问题是我在 webpack 配置中需要 json-loader 并将其作为对象而不是字符串传递,因此将其更改为字符串解决了问题:

loaders: [
  {
    include: /\.json$/,
    loaders: ['json-loader']
  }
]

【讨论】:

    猜你喜欢
    • 2015-06-15
    • 1970-01-01
    • 2020-09-06
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多