Skip to content

Commit

Permalink
Throw an informative error on missing modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 authored and jackfranklin committed Apr 11, 2015
1 parent 2bfc53b commit bb9a5c5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ module.exports = function(options) {
// This searches up from the specified package.json file, making sure
// the config option behaves as expected. See issue #56.
var searchFor = path.join('node_modules', name);
return require(findup(searchFor, {cwd: path.dirname(config)}) || name);

var src = findup(searchFor, {cwd: path.dirname(config)}) || name;
if (src !== null) {
return require(src);
} else {
throw new Error('Cannot find `' + name + '` in your node_modules!');
}
};
} else {
requireFn = require;
Expand Down Expand Up @@ -82,7 +88,7 @@ module.exports = function(options) {

multimatch(names, pattern).forEach(function(name) {
if(scopeTest.test(name)) {
var decomposition = scopeDecomposition.exec(name);
var decomposition = scopeDecomposition.exec(name);

if(!finalObject.hasOwnProperty(decomposition[1])) {
finalObject[decomposition[1]] = {};
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"Julien Fontanet <julien.fontanet@vates.fr>",
"Callum Macrae",
"Hutson Betts",
"Christian Maniewski"
"Christian Maniewski",
"Connor Peet"
],
"license": "MIT",
"dependencies": {
Expand Down
10 changes: 9 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,15 @@ describe('with lazy loading', function() {
});
});

describe('requiring from another directory', function() {
describe('common functionality', function () {
it('throws a sensible error when not found', function () {
var x = gulpLoadPlugins({ config: __dirname + '/package.json' });

assert.throws(function () {
x.oops();
}, /Cannot find module 'gulp-oops'/);
});

it('allows you to use in a lower directory', function() {
var plugins = require('../')();
assert.ok(typeof plugins.test === 'function');
Expand Down
3 changes: 2 additions & 1 deletion test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"gulp-test": "*",
"gulp-test-global": "*"
"gulp-test-global": "*",
"gulp-oops": "*"
}
}

0 comments on commit bb9a5c5

Please sign in to comment.