diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 5a3aaa04e97a09..6a52d55bb84247 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -270,12 +270,14 @@ class AssertionError extends Error { if (message != null) { super(message); } else { - const util = lazyUtil(); - if (process.stdout.isTTY && process.stdout.getColorDepth() !== 1) { + if (util_ === null && + process.stdout.isTTY && + process.stdout.getColorDepth() !== 1) { green = '\u001b[32m'; white = '\u001b[39m'; red = '\u001b[31m'; } + const util = lazyUtil(); if (actual && actual.stack && actual instanceof Error) actual = `${actual.name}: ${actual.message}`; diff --git a/lib/internal/module.js b/lib/internal/module.js index d2140411552429..c3dede40fa3b87 100644 --- a/lib/internal/module.js +++ b/lib/internal/module.js @@ -1,5 +1,7 @@ 'use strict'; +const errors = require('internal/errors'); + // Invoke with makeRequireFunction(module) where |module| is the Module object // to use as the context for the require() function. function makeRequireFunction(mod) { @@ -15,12 +17,20 @@ function makeRequireFunction(mod) { } function resolve(request, options) { + if (typeof request !== 'string') { + throw new errors.Error('ERR_INVALID_ARG_TYPE', + 'request', 'string', request); + } return Module._resolveFilename(request, mod, false, options); } require.resolve = resolve; function paths(request) { + if (typeof request !== 'string') { + throw new errors.Error('ERR_INVALID_ARG_TYPE', + 'request', 'string', request); + } return Module._resolveLookupPaths(request, mod, true); } diff --git a/lib/module.js b/lib/module.js index 4a8480ed1be3af..5ee537f157289b 100644 --- a/lib/module.js +++ b/lib/module.js @@ -603,10 +603,15 @@ Module.prototype.load = function(filename) { // Loads a module at the given file path. Returns that module's // `exports` property. -Module.prototype.require = function(path) { - assert(path, 'missing path'); - assert(typeof path === 'string', 'path must be a string'); - return Module._load(path, this, /* isMain */ false); +Module.prototype.require = function(id) { + if (typeof id !== 'string') { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'id', 'string', id); + } + if (id === '') { + throw new errors.Error('ERR_INVALID_ARG_VALUE', + 'id', id, 'must be a non-empty string'); + } + return Module._load(id, this, /* isMain */ false); }; diff --git a/test/parallel/test-module-loading-error.js b/test/parallel/test-module-loading-error.js index f1c457af2b2c9e..818a35eb582211 100644 --- a/test/parallel/test-module-loading-error.js +++ b/test/parallel/test-module-loading-error.js @@ -59,16 +59,22 @@ assert.throws( } ); -common.expectsError( - require, - { - code: 'ERR_ASSERTION', - message: /^missing path$/ - }); +const re = /^The "id" argument must be of type string\. Received type \w+$/; +[1, false, null, undefined, {}].forEach((value) => { + common.expectsError( + () => { require(value); }, + { + type: TypeError, + code: 'ERR_INVALID_ARG_TYPE', + message: re + }); +}); + common.expectsError( - () => { require({}); }, + () => { require(''); }, { - code: 'ERR_ASSERTION', - message: /^path must be a string$/ + type: Error, + code: 'ERR_INVALID_ARG_VALUE', + message: 'The argument \'id\' must be a non-empty string. Received \'\'' }); diff --git a/test/parallel/test-require-resolve.js b/test/parallel/test-require-resolve.js index 4fbf697faf51b6..2916f3709e3b15 100644 --- a/test/parallel/test-require-resolve.js +++ b/test/parallel/test-require-resolve.js @@ -20,7 +20,7 @@ // USE OR OTHER DEALINGS IN THE SOFTWARE. 'use strict'; -require('../common'); +const common = require('../common'); const fixtures = require('../common/fixtures'); const assert = require('assert'); @@ -38,3 +38,20 @@ assert.strictEqual('path', require.resolve('path')); // Test configurable resolve() paths. require(fixtures.path('require-resolve.js')); require(fixtures.path('resolve-paths', 'default', 'verify-paths.js')); + +const re = /^The "request" argument must be of type string\. Received type \w+$/; +[1, false, null, undefined, {}].forEach((value) => { + common.expectsError( + () => { require.resolve(value); }, + { + code: 'ERR_INVALID_ARG_TYPE', + message: re + }); + + common.expectsError( + () => { require.resolve.paths(value); }, + { + code: 'ERR_INVALID_ARG_TYPE', + message: re + }); +}); diff --git a/test/sequential/test-module-loading.js b/test/sequential/test-module-loading.js index d790850951b064..f0fa933a8ba2b2 100644 --- a/test/sequential/test-module-loading.js +++ b/test/sequential/test-module-loading.js @@ -297,17 +297,6 @@ try { } -// require() must take string, and must be truthy -assert.throws(function() { - console.error('require non-string'); - require({ foo: 'bar' }); -}, /path must be a string/); - -assert.throws(function() { - console.error('require empty string'); - require(''); -}, /missing path/); - process.on('exit', function() { assert.ok(a.A instanceof Function); assert.strictEqual(a.A(), 'A done');