TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string (fs/promises)
Quick answer
You passed something other than a string, Buffer, or URL as the `path` argument to an `fs/promises` function — most often `undefined`, because a variable wasn't populated yet, or a plain object instead of a path string.
What causes this error
Every fs/promises function validates its arguments strictly before touching the filesystem. If path resolves to undefined, null, a number, or a non-path object, Node throws immediately rather than attempting (and possibly mangling) the operation.
The Fix
import { readFile } from 'node:fs/promises';
// Wrong — configPath was never actually set
let configPath;
const data = await readFile(configPath, 'utf-8'); // throws ERR_INVALID_ARG_TYPE
// Fix — make sure the path is actually resolved before use
import path from 'node:path';
const configPath = path.resolve(process.cwd(), 'config.json');
const data = await readFile(configPath, 'utf-8');Common causes / variations
- A path built from `path.join()`/`path.resolve()` where one segment is `undefined`
- Destructuring a config object that doesn't actually have the expected `path` property
- Passing an entire `fs.Dirent` or file-stat object instead of just its `.name`/`.path`
Related errors
See also: ENOENT routes-manifest.json, connect ECONNREFUSED.
Was this fix helpful?
Comments
Loading comments...