Skip to content

Commit

Permalink
ensure @node require loads correctly from cwd
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jun 4, 2016
1 parent 3de7e53 commit 6443c88
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/core.js
Expand Up @@ -114,6 +114,24 @@ function baseURLResolve(loader, name) {
return new URL(name, getBaseURLObj.call(loader)).href;
}

function getNodeModule(name) {
if (!isPlain(name))
throw new Error('Node module ' + name + ' can\'t be loaded as it is not a package require.');

var nodePath = this._nodeRequire('path');
// try to load from node_modules
var module;
try {
module = this._nodeRequire(nodePath.resolve(process.cwd(), 'node_modules', name));
}
catch(e) {
// fall back to direct require (in theory this is core modules only, which should really be filtered)
if (e.code == 'MODULE_NOT_FOUND')
module = this._nodeRequire(name);
}
return module;
}

function coreResolve(name, parentName) {
// standard URL resolution
if (!isPlain(name))
Expand All @@ -135,9 +153,7 @@ function coreResolve(name, parentName) {
if (name.substr(0, 6) == '@node/') {
if (!this._nodeRequire)
throw new TypeError('Error loading ' + name + '. Can only load node core modules in Node.');
if (!isPlain(name.substr(6)))
throw new Error('Node module ' + name.substr(6) + ' can\'t be loaded as it is not a package require.');
this.set(name, this.newModule(getESModule(this._nodeRequire(name.substr(6)))));
this.set(name, this.newModule(getESModule(getNodeModule.call(this, name.substr(6)))));
return name;
}

Expand Down

0 comments on commit 6443c88

Please sign in to comment.