TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined (Node.js) — how to fix
node:internal/validators:162
throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
^
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
at new NodeError (node:internal/errors:405:5)
at validateString (node:internal/validators:162:11)
at Object.join (node:path:1172:7)
code: 'ERR_INVALID_ARG_TYPE'
Quick fix: Read the first stack frame that's in your own code (not node:internal) and log the value passed there. Usually it's process.env.SOMETHING being undefined: set it, or load .env before it's used, or give it a default (process.env.DIR ?? './data'). With react-scripts 3.4.0, upgrade: npm install react-scripts@latest.
What it means
Node checks argument types for its built-in APIs. ERR_INVALID_ARG_TYPE means one of them got the wrong type. The message tells you which argument ("path", "data", "file", "chunk"...) and what was actually passed (Received undefined, Received type number (42), Received an instance of Object).
The frames in node:internal and node:path are just where Node checked the type. Look further down the stack for the first line that points at your project or at a package in node_modules. That's where the bad value came from.
Cause 1: an environment variable or config value is undefined
const uploadDir = path.join(process.env.UPLOAD_DIR, 'images'); // UPLOAD_DIR not set
Fix: log the value, then make sure it's set before the code runs:
console.log('UPLOAD_DIR =', process.env.UPLOAD_DIR);
- Using a
.envfile? Load it before anything reads it:require('dotenv').config()at the very top of the entry file, ornode --env-file=.env app.json Node 20.6+. - In Docker, CI or systemd, set the variable in that environment too. Your shell's
.envfile isn't read there. - Give it a safe default:
const uploadDir = path.join(process.env.UPLOAD_DIR ?? './uploads', 'images');
Cause 2: a value from arguments, JSON or a function is missing
process.argv[2] is undefined when no argument was passed. config.paths.output is undefined when the key is misspelled. A function that forgot to return gives undefined. Validate early with a clear message:
const file = process.argv[2];
if (!file) {
console.error('usage: node script.js <file>');
process.exit(1);
}
Cause 3: the wrong type, not a missing one
fs.writeFile(path, obj)givesThe "data" argument must be of type string or an instance of Buffer... Received an instance of Object. Serialize it first:fs.writeFileSync(file, JSON.stringify(obj, null, 2)).res.write(123)/res.end(123)gives an error about the"chunk"argument. Send a string:res.end(String(123)).- Passing a
URLobject is fine forfsfunctions, butpath.join()only accepts strings. Convert withfileURLToPath(url)fromnode:url.
In ES modules __dirname doesn't exist. Build it like this:
import { fileURLToPath } from 'node:url';
import path from 'node:path';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
Cause 4: an outdated tool (react-scripts 3.4.0 and similar)
If the stack trace is entirely inside node_modules, for example react-scripts/scripts/start.js, the tool has a bug or doesn't support your Node version. A well-known case is react-scripts 3.4.0, which was fixed in 3.4.1:
npm install react-scripts@latest
In general, upgrade the package named in the stack trace, or run the Node version the project was built for (check engines in its package.json, or .nvmrc).
Last updated 2026-12-04.