diff --git a/lib/fs.js b/lib/fs.js index 7741e5838bd..e41d0db0339 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -389,6 +389,9 @@ fs.readFile = function(path, options, callback) { req.oncomplete(null, path); }); return; + } else if (typeof path !== 'string' && !(path instanceof Buffer)) { + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'path', + ['string', 'Buffer', 'URL']); } binding.open(pathModule.toNamespacedPath(path), diff --git a/test/parallel/test-fs-readfile-error.js b/test/parallel/test-fs-readfile-error.js index 616760b0669..9b1a06183d7 100644 --- a/test/parallel/test-fs-readfile-error.js +++ b/test/parallel/test-fs-readfile-error.js @@ -21,6 +21,7 @@ 'use strict'; const common = require('../common'); +const fs = require('fs'); // Test that fs.readFile fails correctly on a non-existent file. @@ -54,3 +55,12 @@ test({ NODE_DEBUG: 'fs' }, common.mustCall((data) => { assert(/EISDIR/.test(data)); assert(/test-fs-readfile-error/.test(data)); })); + +common.expectsError( + () => { fs.readFile(() => {}); }, + { + code: 'ERR_INVALID_ARG_TYPE', + message: 'The "path" argument must be one of type string, Buffer, or URL', + type: TypeError + } +);