{"id":"3639b846fa3966ac","repo":"expressjs/express","slug":"path-argument-is-required-to-res-sendfile","errorCode":null,"errorMessage":"path argument is required to res.sendFile","messagePattern":"path argument is required to res\\.sendFile","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/response.js","lineNumber":381,"sourceCode":" *           res.sendFile('/uploads/' + uid + '/' + file);\n *         } else {\n *           res.send(403, 'Sorry! you cant see that.');\n *         }\n *       });\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);","sourceCodeStart":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/expressjs/express/blob/a3714473feb3d2908add734d340e7755fd85e0a3/lib/response.js#L363-L399","documentation":"Thrown at the top of `res.sendFile` when the `path` argument is falsy (undefined, null, or empty string). Express fails fast here because the underlying `send` module needs a real path, and a missing one usually indicates a caller bug rather than a runtime file condition.","triggerScenarios":"Calling `res.sendFile()` with no arguments, or with a variable that resolved to undefined/null/'' — e.g. `res.sendFile(req.query.file)` when the query param is absent, or a bad destructure of an options object.","commonSituations":"Route handlers that build the path from request input without checking it; refactors that renamed a variable so the argument is undefined; passing the options object as the first argument by mistake.","solutions":["Ensure the first argument is a non-empty path string: guard the request input and return 400 if missing.","Check argument order — the signature is `res.sendFile(path, options, callback)`; the path must come first.","Log the computed path before the call to confirm it isn't undefined at runtime."],"exampleFix":"// before\nres.sendFile(req.query.file);\n// after\nif (!req.query.file) return res.status(400).send('file parameter required');\nres.sendFile(req.query.file, { root: path.join(__dirname, 'public') });","handlingStrategy":"validation","validationCode":"if (!filePath) {\n  return res.status(404).end();\n}\nres.sendFile(filePath, { root: PUBLIC_DIR });","typeGuard":"function hasPath(p) {\n  return p !== undefined && p !== null && p !== '';\n}","tryCatchPattern":null,"preventionTips":["Guard against undefined/empty path before calling res.sendFile — it typically comes from a lookup (req.params, a DB row) that can miss","Fail the request with a 404 when the file lookup returns nothing instead of forwarding a falsy value","Add a test for the route with a missing/unknown file identifier"],"tags":["express","sendfile","validation"],"analyzedSha":"a3714473feb3d2908add734d340e7755fd85e0a3","analyzedAt":"2026-07-31T18:55:36.968Z","schemaVersion":2}