Search code examples
node.jsrequirejsamdcommonjs

In Node v8 (v8.9.3 or above), commonjs require module create new context


We are working on templating library with helpers (Dust.js), the helper library has below design pattern inside the codebase.

(function(root, factory) {
  if (typeof define === 'function' && define.amd && define.amd.dust === true) {
    define(['dust-linkedin'], factory);
  } else if (typeof exports === 'object') {
    module.exports = factory(require('dust-linkedin'));
    // module.exports = factory; // <<--- This is working on v8
  } else {
    factory(root.dust);
  }
}(this, function (dust) {
 ...
});

In node v6, exporting factory(require('dust-linkedin')) is working fine, but after switching to node v8 (v8.9.3) and we see helpers are not getting executed, changing factory(require('dust-linkedin')) to factory inside the helper library, things starts working.

In node v8, when require('dust-linkedin') does it create a new context/object because of which we are losing all our helpers? Is there any change in commonjs require behavior?


Solution

  • The solution was fixed in the PR https://github.com/krakenjs/festucam/pull/4