Skip to content

Node.js error

Node.js: “TypeError [ERR_INVALID_ARG_TYPE]: The 'path' argument must be of type string”

A script or build fails with TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined (or "data" must be of type string or an instance of Buffer, or "cb" must be of type function). A built-in Node.js function such as path.join, fs.readFile or Buffer.from got a value of the wrong type. The message names the argument and says what it received, which points straight at the cause.

Rather hand it over? App Fix Ticket: One reproducible bug in an existing mobile or web app, diagnosed and fixed.The Fix is $1,200 in total. The $450 Diagnosis is optional and credited toward it.

Full refund if Simon can’t take it on once he sees your project, or if you cancel before work starts. Refund terms.

Short answer

Read the last part of the message. Received undefined means a variable, environment variable or config value is not set; trace it back and set it, or fail early with a clear error. Received an instance of Object or a number means you passed the wrong thing (an object where a string or Buffer belongs, so stringify it). If the stack trace is inside a build tool such as an old react-scripts or webpack, the tool is too old for your Node.js version: upgrade it.

Rather not fix it yourself? App Fix Ticket fixes one reproducible bug at a fixed price, with an optional Diagnosis that is credited toward the fix.

What the message received, and the fix

  1. 01

    Received undefined: a value that was never set

    “A TypeError is thrown if path is not a string.” Node.js, path: path.dirname()

    Most often an environment variable, a config key or a function argument that is missing, passed into path.join or path.resolve. Check the value before you use it, so the error names the real problem.

    const dir = process.env.UPLOAD_DIR;
    if (!dir) throw new Error('UPLOAD_DIR is not set');
    const file = path.join(dir, name);
  2. 02

    Received an instance of Object, or a number

    “An argument of the wrong type was passed to a Node.js API.” Node.js, Errors: ERR_INVALID_ARG_TYPE

    fs.writeFile, res.write and Buffer.from take a string or a Buffer, not an object or a number. Convert the value first.

    // wrong
    fs.writeFileSync('out.json', data);
    
    // right
    fs.writeFileSync('out.json', JSON.stringify(data, null, 2));
    res.end(String(count));
  3. 03

    The "cb" argument must be of type function

    The callback versions of fs functions require a callback. Use the promise version or the Sync version when you do not want one.

    import { writeFile } from 'node:fs/promises';
    await writeFile('out.txt', text);
  4. 04

    An old build tool on a newer Node.js

    When the stack trace points into node_modules (react-scripts, webpack, an old Gulp or Babel plugin), the tool calls a Node.js API in a way newer versions reject. Upgrade that package to a release that supports your Node.js version, or run the Node.js LTS version it was built for.

Checked against the vendor’s docs on : Node.js: Errors (ERR_INVALID_ARG_TYPE); Node.js: path.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

“Very responsive, attentive to fixing bugs, and completed the project as needed! Thank you!”
A buyer in the United States, July 2022. Shopify on Fiverr
Read 126 of the 127 Fiverr reviews

Related: Node.js “ENOENT: no such file or directory” · Node.js “Cannot find module” · “Cannot read properties of undefined” · Node.js ERR_MODULE_NOT_FOUND

Questions

More on this error

Why does it only fail in production or CI?

The environment variable or config file the code reads exists on your machine but not on the server. Add it to the hosting provider’s environment settings, and validate required variables when the app starts.

Does a newer Node.js version cause it?

It can expose it. Newer Node.js versions validate arguments more strictly, so code or tools that passed the wrong type silently before now throw. Fix the call, or upgrade the tool.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it. If you already know the cause, you can pay for the fix directly; if not, start with a written diagnosis, which is credited toward the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Ask Simon for a price range

Send the error and what you were doing. Simon replies by email, usually within one business day, with a price range.