TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
Node.js · updated 2026-11-03
Thrown synchronously by Node.js core APIs (fs, path, crypto, Buffer, http) when an argument has the wrong type. The message tells you which argument, what it should be and what arrived, which is usually enough to find the bad value.
node:internal/errors:540
throw error;
^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
at Object.join (node:path:1253:7)
at loadTemplates (file:///app/src/email.js:9:20) {
code: 'ERR_INVALID_ARG_TYPE'
}Quick fix
Look at the "Received" part: undefined almost always means a missing environment variable, config key or function argument. Find where that value comes from (often process.env.SOMETHING) and make sure it is set in this environment, or fail early with a clear message.
- Read which argument it names ("path", "data", "key", "chunk") and what was received.
- Follow the value back from the failing call: env variable, config file, request body, function parameter.
- For env variables, check they are set in the deployed environment, not only in your local .env.
- Validate required config at startup so the app fails with the variable name instead of deep inside fs or path.
- For "Received an instance of Object", check you passed the field (obj.path) rather than the whole object.
// config.ts: fail at startup with the name of the missing value
function required(name: string): string {
const v = process.env[name];
if (!v) throw new Error(`Missing environment variable ${name}`);
return v;
}
export const config = {
templatesDir: required('TEMPLATES_DIR'),
signingKey: required('SIGNING_KEY'),
};Why it happens
A missing environment variable
process.env.X is undefined when the variable is not set where the app runs. path.join, fs.readFile and crypto.createHmac then reject it with this error.
Passing an object instead of a field
Calling fs.readFile(file) when file is { path, name } gives "Received an instance of Object". The function wanted the string inside.
Writing a number or object to a stream or response
res.write(123), res.end({ ok: true }) or Buffer.from(undefined) need a string or Buffer; serialize first with String() or JSON.stringify().
An optional value that is sometimes missing
A request field, query parameter or database column that is usually present but occasionally undefined makes the error intermittent and hard to reproduce.
Other fixes
- Use a schema library (zod, valibot) to parse process.env once at startup.
- In TypeScript, avoid non-null assertions (!) on env values: they turn a compile error into this runtime error.
Same error, other wording
- TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
- TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey. Received undefined
- TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received undefined
Let your coding agent catch the next one
Runheed records this error in production with the stack trace, the breadcrumbs before it and the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:
> Find the ERR_INVALID_ARG_TYPE errors in production, show me which argument received what and where that value comes from, and add startup validation for any environment variables involved.
Related errors
- TypeError: Cannot read properties of undefined (reading 'x')
- Error: ENOENT: no such file or directory, open '/app/config.json'
- TypeError: x is not a function
References: Node.js errors: ERR_INVALID_ARG_TYPE, Node.js: process.env