Skip to content

Instantly share code, notes, and snippets.

@isaacs
Created March 29, 2012 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaacs/2241076 to your computer and use it in GitHub Desktop.
Save isaacs/2241076 to your computer and use it in GitHub Desktop.
diff --git a/lib/module.js b/lib/module.js
index f6f6285..ea5a18f 100644
--- a/lib/module.js
+++ b/lib/module.js
@@ -440,8 +440,22 @@ Module.prototype._compile = function(content, filename) {
global.v8debug.Debug.setBreakPoint(compiledWrapper, 0, 0);
}
}
- var args = [self.exports, require, self, filename, dirname];
- return compiledWrapper.apply(self.exports, args);
+ var exports = self.exports;
+ var args = [exports, require, self, filename, dirname];
+ var ret = compiledWrapper.apply(self.exports, args);
+
+ // if the wrapper returns a value, and has not already
+ // set module.exports to something different, and has not
+ // added any keys to exports, then use the return value as
+ // the exported module result.
+ if (ret !== undefined && self.exports === exports &&
+ Object.keys(exports).length === 0) {
+ self.exports = ret;
+ }
+
+ // always return the return of the compilation.
+ // this is needed for node --eval
+ return ret;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment