在 webpack 中使用 moment-timezone
Using moment-timezone with webpack
我有 react
/redux
应用程序在 webpack
上运行并且想使用 moment-timezone
库。我已经在 SO 中检查了 问题并按照问题中的描述安装了 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]
}
]
我的问题是在 webpack
配置中。问题是我在 webpack 配置中需要 json-loader
并将其作为对象而不是字符串传递,因此将其更改为字符串解决了问题:
loaders: [
{
include: /\.json$/,
loaders: ['json-loader']
}
]
我有 react
/redux
应用程序在 webpack
上运行并且想使用 moment-timezone
库。我已经在 SO 中检查了 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]
}
]
我的问题是在 webpack
配置中。问题是我在 webpack 配置中需要 json-loader
并将其作为对象而不是字符串传递,因此将其更改为字符串解决了问题:
loaders: [
{
include: /\.json$/,
loaders: ['json-loader']
}
]