icambron/twix.js

Wrong behavior in node js when another module depends from old moment version

vdmitriy opened this issue · 5 comments

Steps to reproduce:

  1. Create simple node js app. I use node version 0.10.38
  2. Install latest versions of moment (2.10.2), twix(0.6.3) and any module that depends from old moment version. I use file-stream-rotator module. This module requires moment version 2.3.1
  3. Require this modules in next order:
var moment = require('moment');
var FileStreamRotator = require('file-stream-rotator');
var twix = require('twix');

After starting app, you got error

[TypeError: Object function (input, format, lang, strict) {
        if (typeof(lang) === "boolean") {
            strict = lang;
            lang = undefined;
        }
        return makeMoment({
            _i : input,
            _f : format,
            _l : lang,
            _strict : strict,
            _isUTC : false
        });
    } has no method 'locale']

It occurs, because Twix will be initialized twice:

if (hasModule) {  
    module.exports = makeTwix(require("moment"));
  }
else if (typeof moment !== "undefined" && moment !== null) {    
    this.Twix = makeTwix(moment);
  }

You can check it also here

Twix is only getting initialized once, but it's being provided with the older version of Moment (the older one uses lang() instead of locale()). If you swap the order of requires so that Twix goes right after Moment, it works. If you know of a way to make sure Moment gets the version of the library it asks for in package.json and not just whatever one got required last, I'd be open to it. I don't really understand how Node dependency management works.

Node dependency management works linear in order when you require modules. If some module has dependencies from another modules, I can't change its versions. About swap order - it's not good solution, because project may contains many files with requires.
I uploaded sample node js project to google drive. Please download it and try to run with command

npm start

So, you can check, that twix will be initialized twice, if some module depends from old version of moment. I added debug log in file /bin/twix.js

If you don't have a time to analize and fix this problem, I may try to do it self, ok?

I haven't gotten a chance to look at the project. I still don't understand how the issue is it loading twice, or why that would have the effect your seeing. Happy to look at a PR though!

I created video, how twix loading twice. Please look

Fixed by #69.