Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using moment-timezone with webpack

I have react/redux application that runs on webpack and would like to use moment-timezone library. I already checked How should I use moment-timezone with webpack? issue here in SO and installed json-loader as it is described in the issue. But still when requiring moment-timezone in my application as:

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

it raises an error:

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

Where the @ ./~/moment-timezone/index.js 2:15-51 is:

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

However when I included the minified version from the build folder it worked properly, as:

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

Update:

webpack configs:

let jsonLoader = require('json-loader');
loaders: [
  {
    include: /\.json$/,
    loaders: [jsonLoader]
  }
]
like image 337
Max Avatar asked Oct 22 '15 09:10

Max


1 Answers

My issue was that in the webpack configurations. The problem was that I required the json-loader in the webpack config and passed it as an object instead of string, so changing it to string fixed the issue:

loaders: [
  {
    include: /\.json$/,
    loaders: ['json-loader']
  }
]
like image 62
Max Avatar answered Oct 04 '22 19:10

Max