Skip to content

Commit bb9a5c5

Browse files
connor4312jackfranklin
authored andcommitted
Throw an informative error on missing modules.
1 parent 2bfc53b commit bb9a5c5

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ module.exports = function(options) {
3434
// This searches up from the specified package.json file, making sure
3535
// the config option behaves as expected. See issue #56.
3636
var searchFor = path.join('node_modules', name);
37-
return require(findup(searchFor, {cwd: path.dirname(config)}) || name);
37+
38+
var src = findup(searchFor, {cwd: path.dirname(config)}) || name;
39+
if (src !== null) {
40+
return require(src);
41+
} else {
42+
throw new Error('Cannot find `' + name + '` in your node_modules!');
43+
}
3844
};
3945
} else {
4046
requireFn = require;
@@ -82,7 +88,7 @@ module.exports = function(options) {
8288

8389
multimatch(names, pattern).forEach(function(name) {
8490
if(scopeTest.test(name)) {
85-
var decomposition = scopeDecomposition.exec(name);
91+
var decomposition = scopeDecomposition.exec(name);
8692

8793
if(!finalObject.hasOwnProperty(decomposition[1])) {
8894
finalObject[decomposition[1]] = {};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"Julien Fontanet <[email protected]>",
2727
"Callum Macrae",
2828
"Hutson Betts",
29-
"Christian Maniewski"
29+
"Christian Maniewski",
30+
"Connor Peet"
3031
],
3132
"license": "MIT",
3233
"dependencies": {

test/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,15 @@ describe('with lazy loading', function() {
198198
});
199199
});
200200

201-
describe('requiring from another directory', function() {
201+
describe('common functionality', function () {
202+
it('throws a sensible error when not found', function () {
203+
var x = gulpLoadPlugins({ config: __dirname + '/package.json' });
204+
205+
assert.throws(function () {
206+
x.oops();
207+
}, /Cannot find module 'gulp-oops'/);
208+
});
209+
202210
it('allows you to use in a lower directory', function() {
203211
var plugins = require('../')();
204212
assert.ok(typeof plugins.test === 'function');

test/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"dependencies": {
33
"gulp-test": "*",
4-
"gulp-test-global": "*"
4+
"gulp-test-global": "*",
5+
"gulp-oops": "*"
56
}
67
}

0 commit comments

Comments
 (0)