diff --git a/lib/fs.js b/lib/fs.js index e41d0db03390b0..93be37533405da 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -340,6 +340,10 @@ fs.accessSync = function(path, mode) { fs.exists = function(path, callback) { if (handleError((path = getPathFromURL(path)), cb)) return; + if (typeof path !== 'string' && !(path instanceof Buffer)) { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'path', + ['string', 'Buffer', 'URL']); + } if (!nullCheck(path, cb)) return; var req = new FSReqWrap(); req.oncomplete = cb; @@ -361,6 +365,9 @@ Object.defineProperty(fs.exists, internalUtil.promisify.custom, { fs.existsSync = function(path) { try { handleError((path = getPathFromURL(path))); + if (typeof path !== 'string' && !(path instanceof Buffer)) { + return false; + } nullCheck(path); binding.stat(pathModule.toNamespacedPath(path)); return true; diff --git a/test/parallel/test-fs-exists.js b/test/parallel/test-fs-exists.js index 331c8c04e38e78..a526373c6a876d 100644 --- a/test/parallel/test-fs-exists.js +++ b/test/parallel/test-fs-exists.js @@ -42,3 +42,14 @@ fs.exists(new URL('https://foo'), common.mustCall(function(y) { assert(fs.existsSync(f)); assert(!fs.existsSync(`${f}-NO`)); + +common.expectsError( + () => { fs.exists(() => {}); }, + { + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "path" argument must be one of type string, Buffer, or URL', + type: TypeError + } +); + +assert(!fs.existsSync());