{"id":"63fd865fb5001160","repo":"expressjs/express","slug":"path-must-be-a-string-to-res-sendfile","errorCode":null,"errorMessage":"path must be a string to res.sendFile","messagePattern":"path must be a string to res\\.sendFile","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/response.js","lineNumber":385,"sourceCode":" *       });\n *     });\n *\n * @public\n */\n\nres.sendFile = function sendFile(path, options, callback) {\n  var done = callback;\n  var req = this.req;\n  var res = this;\n  var next = req.next;\n  var opts = options || {};\n\n  if (!path) {\n    throw new TypeError('path argument is required to res.sendFile');\n  }\n\n  if (typeof path !== 'string') {\n    throw new TypeError('path must be a string to res.sendFile')\n  }\n\n  // support function as second arg\n  if (typeof options === 'function') {\n    done = options;\n    opts = {};\n  }\n\n  if (!opts.root && !pathIsAbsolute(path)) {\n    throw new TypeError('path must be absolute or specify root to res.sendFile');\n  }\n\n  // create file stream\n  var pathname = encodeURI(path);\n\n  // wire application etag option to send\n  opts.etag = this.app.enabled('etag');\n  var file = send(req, pathname, opts);","sourceCodeStart":367,"sourceCodeEnd":403,"githubUrl":"https://github.com/expressjs/express/blob/a3714473feb3d2908add734d340e7755fd85e0a3/lib/response.js#L367-L403","documentation":"Thrown by `res.sendFile` when a truthy `path` argument is not a string (e.g. a Buffer, number, array, or object). The path is later passed to `encodeURI` and the `send` module, both of which require a string, so Express rejects other types up front.","triggerScenarios":"`res.sendFile(fs.readFileSync(p))` (passing file contents instead of the path), `res.sendFile(['a.txt'])`, `res.sendFile({ path: p })`, or passing a URL/Path object instead of a string.","commonSituations":"Confusing sendFile (takes a path) with res.send (takes a body); passing a `URL` or `path`-like object from a library; array-valued query params (`?file=a&file=b`) producing an array from req.query.","solutions":["Pass the filesystem path string, not the file's contents or an object: `res.sendFile('/abs/path/file.txt')`.","If using a URL or Path-like object, convert it first (e.g. `fileURLToPath(url)` or `String(p)`).","If the value comes from req.query, reject non-string values (repeat params arrive as arrays)."],"exampleFix":"// before\nres.sendFile(fs.readFileSync(filePath));\n// after\nres.sendFile(filePath); // sendFile reads the file itself","handlingStrategy":"type-guard","validationCode":"if (typeof filePath !== 'string') {\n  return next(new TypeError('resolved file path is not a string'));\n}\nres.sendFile(filePath, { root: PUBLIC_DIR });","typeGuard":"function isPathString(p) {\n  return typeof p === 'string' && p.length > 0;\n}","tryCatchPattern":null,"preventionTips":["req.query values can be arrays (?file=a&file=b) or objects — never assume a string; check typeof first","Reject non-string path inputs at the route boundary with a 400 rather than passing them through","Use a validation library (zod/joi) on request inputs that feed file paths"],"tags":["express","sendfile","type-error"],"analyzedSha":"a3714473feb3d2908add734d340e7755fd85e0a3","analyzedAt":"2026-07-31T18:55:36.968Z","schemaVersion":2}