Node fs rmdir TypeError 'Callback must be a function'

Viewed 1398

When using the fs.rmdir function I get this error from Ubuntu:

fs.js:136
     throw new ERR_INVALID_CALLBACK();
     ^
TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
     at makeCallback (fs.js:136:11)
     at Object.rmdir (fs.js:671:14)
     <...>

I'm not sure what the cause of this is, since it works perfectly fine when I test it locally on my Windows 10 computer.
Here's the code that is causing the error:

// remove client's temporary directory and its files
fs.rmdir('temp/' + socketID, {recursive: true}, (error) => {
    if(error) throw error;
});

Also, on the Ubuntu machine, if I remove the {recursive: true} option, the command does run and the callback works, but it doesn't solve the problem since I want the recursive option.

1 Answers

You are apparently running an older version of nodejs on that machine where it doesn't work that does not support the 2nd argument for options and thus it thinks you're passing the options object in the place where it expects the callback.

Check your nodejs version. You will need at least v12.10 to get support for the recursive option. Based on the error you report, it looks like you have a version of v10 or greater, but less than v12.10. Update your nodejs installation and you should be good.

Here's the development history for fs.rmdir():

v13.3.0, v12.16.0   
The maxBusyTries option is renamed to maxRetries, and its default is 0. 
The emfileWait option has been removed, and EMFILE errors use the same retry logic as 
other errors. The retryDelay option is now supported. ENFILE errors are now retried.

v12.10.0    
The recursive, maxBusyTries, and emfileWait options are now supported.

v10.0.0 
The callback parameter is no longer optional. Not passing it will throw a TypeError 
at runtime.

v7.6.0  
The path parameters can be a WHATWG URL object using file: protocol. Support is currently 
still experimental.

v7.0.0  
The callback parameter is no longer optional. Not passing it will emit a deprecation 
warning with id DEP0013.

v0.0.2  
Added in: v0.0.2
Related