mde/timezone-js

Browserify require timezone-js raise uncaught error

Closed this issue · 4 comments

When use browserify in front end code:

var timezoneJS = require('timezone-js');

There are js uncought error in console says:
"Uncaught Error: Please use the Fleegix.js XHR module, jQuery ajax, Zepto ajax, or define your own transport mechanism for downloading zone files. "

This is due to the 'this' variable used in src/date.js is an empty object if using browserify to require 'timezone-js'. A simple PR would be instead:

(function () {
  // Standard initialization stuff to make sure the library is
  // usable on both client and server (node) side.
  'use strict';
  var root = this;
...
var ajax_lib = root.$ || root.jQuery || root.Zepto
    , fleegix = root.fleegix
    // Declare constant list of days and months. Unfortunately this doesn't leave room for i18n due to the Olson data being in English itself
...
// Client-side
   if ((!fleegix || typeof fleegix.xhr === 'undefined') && (!ajax_lib || typeof ajax_lib.ajax === 'undefined')) {
      throw new Error('Please use the Fleegix.js XHR module, jQuery ajax, Zepto ajax, or define your own transport mechanism for downloading zone files.');
    }
...
}).call(this);

use

(function () {
  // Standard initialization stuff to make sure the library is
  // usable on both client and server (node) side.
  'use strict';
  var root = this;
...
}).call(typeof window !== "undefined" ? window : this);

See #159

Did you declare jquery as a dependency for timezonejs in your browserify shim?

@bcherny no -- there is npm version of jquery which I don't have to use browserify shim.

I don't mean shim jquery, but shim timezoneJS. So in your package.json:

  "browserify-shim": {
    "./node_modules/timezonejs/timezone.js": {
      "exports": "timezonejs",
      "depends": [ "./node_modules/jquery/jquery.js:$" ]
    }
  }

see https://github.com/thlorenz/browserify-shim#a-config-inside-packagejson-without-aliases

mde commented

Does PR #159 fix this? Can we close this issue?