Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

fs.writeFile throws ERR_INVALID_ARG_TYPE for the data argument

ErrorTypeError [ERR_INVALID_ARG_TYPE]

Short answer

fs.writeFile accepts a string, Buffer, TypedArray or DataView as data. Since Node.js 14 it no longer turns other values into strings. Convert an object with JSON.stringify and a number with String, and find where an undefined value or a Promise came from.

  • Applies to Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The end of the message names what you really passed, so read it. Print the value and its type right before the write call.

    console.log(typeof data, data);
    fs.writeFileSync('out.json', data);
  2. 2Match what you see, then jump to its fix

    1. 1The value you print is an object or an arrayYou passed an object or an arrayFix
    2. 2typeof prints number or booleanYou passed a number, a boolean or another primitiveFix
    3. 3The value you print is undefined, or a PromiseThe value is undefined, or a Promise, because an async result was not awaitedFix

What does the error look like?

  • TypeError [ERR_INVALID_ARG_TYPE]
  • The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView

Cause 1 of 3

You passed an object or an array

1Check

The data type list of fs.writeFile has no object. Since Node.js 14 the call no longer turns unsupported values into strings.

console.log(Object.prototype.toString.call(data));

2Fix

Turn the value into JSON text yourself, with indentation if people will read the file.

const fs = require('node:fs');
fs.writeFileSync('out.json', JSON.stringify(data, null, 2));

3Verify

The file is written and holds valid JSON.

node -e "JSON.parse(require('node:fs').readFileSync('out.json', 'utf8')); console.log('ok')"

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 3

You passed a number, a boolean or another primitive

1Check

Only strings and binary types are accepted. A count or a flag is not written as text for you.

console.log(typeof data);

2Fix

Convert it to a string on purpose.

fs.writeFileSync('count.txt', String(count));

3Verify

The file contains the number as text.

cat count.txt

Confirmed by nodejs.org

Did this fix it?

Cause 3 of 3

The value is undefined, or a Promise, because an async result was not awaited

1Check

A function that forgot to return, or an async function whose result was not awaited, hands the write call undefined or a Promise. Neither is in the list of accepted types.

console.log(data instanceof Promise);
console.log(data === undefined);

2Fix

Await the call that produces the data, and make sure the function returns it. Then write.

const fs = require('node:fs/promises');
const data = await buildReport();
await fs.writeFile('report.json', JSON.stringify(data));

3Verify

The value you print is no longer undefined or a Promise, and the write succeeds.

console.log(typeof data);

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Treat file writes as the place where data becomes text: stringify there, once, and nowhere else.
  • Type-check or lint the project (TypeScript or JSDoc types) so a wrong argument type is caught before the program runs.
  • Always await async functions, and enable a lint rule that flags floating promises.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.