Maaz Zindani

Loading the index…

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

fs.writeFile throws ERR_INVALID_ARG_TYPE for the callback

ErrorThe "cb" argument must be of type function

Short 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.

  • 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

    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/
  2. 2Match what you see, then jump to its fix

    1. 1The stack trace points at a fs.writeFile(path, data) line with two argumentsThe call has no callback at allFix
    2. 2The call looks like fs.writeFile(path, data, 'utf8') with no function after itThe options string sits where the callback should beFix
    3. 3The 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

What does the error look like?

  • The "cb" argument must be of type function
  • TypeError [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.txt

Confirmed by nodejs.org

Did this fix it?

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.js

Confirmed by nodejs.org

Did this fix it?

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.txt

Confirmed by nodejs.org

Did this fix it?

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 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.