Skip to content

Commit

Permalink
Fix require node module, #246
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamsc committed Sep 24, 2019
1 parent 7a3a5db commit ab7a1e2
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/req-manager.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
var mod = require("module"),
path = require("path"),
fs = require("fs"),
logger = require("./logger");

var Req = function (filepath, root) {
this.Module = mod.Module;
this.root = root;
this.filepath = filepath || null;
this.nativeModules = [
"assert", "buffer", "child_process", "constants", "crypto", "tls", "dgram", "dns", "http", "https",
"assert", "buffer", "child_process", "constants", "crypto", "tls", "dgram", "dns", "http", "https",
"net", "querystring", "url", "domain", "events", "fs", "path", "os", "punycode", "stream", "string_decoder",
"timers", "tty", "util", "sys", "vm", "zlib"
];
Expand All @@ -31,7 +32,14 @@ Req.prototype.load = function (modname) {
path.join(self.root, "node_modules")
].concat(module.paths.filter(function (p) { return p.indexOf(".gauge") < 0; }));
try {
return modname === path.basename(modname) ? m.require(modname) : m.require(path.join(path.dirname(self.filepath), modname));
if (modname === path.basename(modname)) {
return m.require(modname);
}
let relativePath = path.join(path.dirname(self.filepath), modname);
if (fs.existsSync(relativePath)) {
return m.require(relativePath);
}
return m.require(modname);
} catch (e) {
logger.error("Unable to require module '" + modname + "' in " + self.filepath + "\n" + e.stack);
return null;
Expand Down

0 comments on commit ab7a1e2

Please sign in to comment.