fs.writeFile throws ERR_INVALID_ARG_TYPE for the data argument
TypeError [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.
Fix it fast
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);2Match what you see, then jump to its fix
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
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.txtConfirmed by nodejs.org
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
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
fs.writeFile throws ERR_INVALID_ARG_TYPE for the callback
TypeError [ERR_INVALID_ARG_TYPE]
Since Node.js 10 the callback of fs.writeFile is required, and an invalid or missing one throws ERR_INVALID_ARG_TYPE.
3 causes, each with a fixBack end
Writing a file in Node.js when its parent folder does not exist yet
EEXIST (File exists) from fs.mkdir
fs.writeFile creates the file, not the folders above it.
2 causes, each with a fixBack 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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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