ERR_INVALID_ARG_TYPE node type_error ai_generated true

TypeError [ERR_INVALID_ARG_TYPE]: The argument must be of type string

ID: node/err-invalid-arg-type

Also available as: JSON · Markdown
90%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
20 active

Root Cause

Wrong argument type passed to Node.js API. Common with Buffer/string confusion.

generic

Workarounds

  1. 92% success Check the calling code — trace where wrong type originates
    Check the calling code — trace where wrong type originates

    Sources: https://nodejs.org/api/errors.html#err_invalid_arg_type

  2. 85% success Add TypeScript or JSDoc types to catch at compile time
    // Add JSDoc types:
    /** @param {string} filePath */
    function readFile(filePath) { ... }
    // Or use TypeScript: npx tsc --init && npx tsc --noEmit

    Sources: https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html

Dead Ends

Common approaches that don't work:

  1. Force cast with String() or Buffer.from() 60% fail

    Hides the real source of wrong type

  2. Use any type and bypass checks 70% fail

    Pushes error downstream

Error Chain

Frequently confused with: