类型错误:删除目录时回调不是函数

La-*_*-go 4 javascript directory fs node.js

当我尝试删除现有目录(及其内部的文件)时,fs.rm('./temp/', { recursive: true, force: true });成功删除了它,但会抛出此错误:

node:internal/fs/rimraf:60
    callback(err);
    ^

TypeError: callback is not a function
    at CB (node:internal/fs/rimraf:60:5)
    at FSReqCallback.oncomplete (node:fs:188:23)
Run Code Online (Sandbox Code Playgroud)

我安装了 Node v16.13.2。谢谢!

小智 5

由于node.js fs文档https://nodejs.org/api/fs.html#fsrmpath-options-callback

你必须传递一个回调函数 fs.rm(path[, options], callback)

所以你的代码将如下所示:

fs.rm('./temp/', { recursive: true, force: true }, (error) => {
    //you can handle the error here
});
Run Code Online (Sandbox Code Playgroud)