expressjs/express · error · TypeError
name argument is required to req.get
Error message
name argument is required to req.get
What it means
Express's req.get() (aliased req.header()) reads a request header by name. At lib/request.js:65-67 it throws a TypeError when the name argument is falsy (undefined, null, or empty string), because looking up a header without a name is always a programming mistake rather than a runtime condition Express can recover from.
Source
Thrown at lib/request.js:66
* // => "text/plain"
*
* req.get('content-type');
* // => "text/plain"
*
* req.get('Something');
* // => undefined
*
* Aliased as `req.header()`.
*
* @param {String} name
* @return {String}
* @public
*/
req.get =
req.header = function header(name) {
if (!name) {
throw new TypeError('name argument is required to req.get');
}
if (typeof name !== 'string') {
throw new TypeError('name must be a string to req.get');
}
var lc = name.toLowerCase();
switch (lc) {
case 'referer':
case 'referrer':
return this.headers.referrer
|| this.headers.referer;
default:
return this.headers[lc];
}
};
View on GitHub ↗ (pinned to a3714473fe)
Solutions
- Pass an explicit header name string, e.g. req.get('content-type').
- Trace where the argument comes from and guard against the config value being undefined (fail fast at startup if a required header name isn't configured).
- If you want all headers, use req.headers directly instead of req.get().
Example fix
// before
const auth = req.get(process.env.AUTH_HEADER); // AUTH_HEADER unset -> undefined
// after
const headerName = process.env.AUTH_HEADER;
if (!headerName) throw new Error('AUTH_HEADER env var is required');
const auth = req.get(headerName); Defensive patterns
Strategy: validation
Validate before calling
if (headerName === undefined || headerName === null) {
throw new Error('header name is required before calling req.get');
}
const value = req.get(headerName); Type guard
function hasHeaderName(name) {
return name !== undefined && name !== null;
} Try / catch
try {
const value = req.get(headerName);
} catch (err) {
if (/name argument is required/.test(err.message)) {
// caller passed undefined/null — fix the call site
}
throw err;
} Prevention
- Never pass a variable to req.get() without checking it is defined — undefined header names usually come from optional config or destructuring typos.
- Use a constant for header names (e.g. const HDR_AUTH = 'authorization') instead of building them dynamically.
- Add a lint rule or wrapper helper that asserts the header name is present before delegating to req.get.
When it happens
Trigger: Calling req.get() or req.header() with no argument, or passing a variable that evaluates to undefined/null/'' — e.g. req.get(config.headerName) where headerName was never set, or req.get() during a refactor that dropped the argument.
Common situations: Header names sourced from environment variables or config objects that are missing in a deployment environment; destructured options with a typo'd key; code migrated from frameworks where a no-arg call returned all headers. Note Express 4 threw the same error, so this is rarely a version-upgrade issue.
Related errors
- name must be a string to req.get
- app.use() requires a middleware function
- Invalid status code: ${JSON.stringify(code)}. Status code mu
- callback function required
- Failed to lookup view "' + name + '" in views ' + dirs
AI-assisted analysis of expressjs/express@a3714473fe (2026-07-31).
Data as JSON: /data/errors/03e58cd4b5b895ac.json.
Report an issue: GitHub ↗.