fs.writeFile throws ERR_INVALID_ARG_TYPE for the callback
The "cb" argument must be of type functionShort answer
Since Node.js 10 the callback of fs.writeFile is required, and an invalid or missing one throws ERR_INVALID_ARG_TYPE. Either pass a callback as the last argument, or use the promise version from node:fs/promises with await, which needs no callback.
Fix it fast
1Find out which cause you have
Look at the call in the stack trace and count the arguments. The callback must be the last one, after the optional options argument.
grep -rn "writeFile(" src/2Match what you see, then jump to its fix
- 1
The stack trace points at a fs.writeFile(path, data) line with two argumentsThe call has no callback at allFix - 2
The call looks like fs.writeFile(path, data, 'utf8') with no function after itThe options string sits where the callback should beFix - 3
The code does await fs.writeFile(path, data) and uses require('fs') or import fs from 'node:fs'You wanted await but imported the callback APIFix
- 1
What does the error look like?
The "cb" argument must be of type functionTypeError [ERR_INVALID_ARG_TYPE]
Cause 1 of 3
The call has no callback at all
1Check
Older code and old tutorials call fs.writeFile with just a path and data. The Node.js history for this function says the callback is no longer optional since v10.0.0 and that omitting it throws a TypeError.
grep -rn "fs.writeFile(" src/2Fix
Add the callback as the final argument and handle its error.
const fs = require('node:fs');
fs.writeFile('out.txt', 'hello', (err) => {
if (err) throw err;
console.log('saved');
});3Verify
Run the script; the file exists and the message prints.
node app.js && cat out.txtConfirmed by nodejs.org
Cause 2 of 3
The options string sits where the callback should be
1Check
The signature is writeFile(file, data[, options], callback). A string in the third slot is not a callback, so the call is still missing one. Node.js 18 and later report an invalid callback as ERR_INVALID_ARG_TYPE.
grep -rn "writeFile(" src/2Fix
Keep options as the third argument and add the callback as the fourth.
fs.writeFile('out.txt', 'hello', { encoding: 'utf8' }, (err) => {
if (err) throw err;
});3Verify
Run the script again and expect no TypeError.
node app.jsConfirmed by nodejs.org
Cause 3 of 3
You wanted await but imported the callback API
1Check
The callback API is designed to be given a function. The promise-based writeFile lives in node:fs/promises and returns a promise that fulfills with undefined.
grep -rn "await fs.writeFile" src/2Fix
Import the promise API and keep the await.
import { writeFile } from 'node:fs/promises';
await writeFile('out.txt', 'hello', { encoding: 'utf8' });3Verify
The script finishes and the file has the text.
node app.mjs && cat out.txtConfirmed by nodejs.org
How do I stop it coming back?
- Import from node:fs/promises in new code, so there is no callback to forget.
- Turn on an ESLint rule for floating promises, so a missing await is flagged.
- Read the Node.js docs for the exact argument order when copying a snippet from an old tutorial.
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 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix