【问题标题】:Node fs rmdir TypeError 'Callback must be a function'Node fs rmdir TypeError '回调必须是一个函数'
【发布时间】:2021-05-02 00:45:07
【问题描述】:

使用 fs.rmdir 函数时,我从 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)
     <...>

我不确定这是什么原因,因为当我在我的 Windows 10 计算机上进行本地测试时,它工作得非常好。
这是导致错误的代码:

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

另外,在 Ubuntu 机器上,如果我删除 {recursive: true} 选项,命令会运行并且回调可以工作,但它不能解决问题,因为我想要递归选项。

【问题讨论】:

    标签: node.js callback fs


    【解决方案1】:

    您显然在该机器上运行旧版本的 nodejs,它不支持选项的第二个参数,因此它认为您正在将选项对象传递到它期望回调的位置。

    检查你的 nodejs 版本。您至少需要 v12.10 才能获得对递归选项的支持。根据您报告的错误,您的版本似乎是 v10 或更高版本,但低于 v12.10。更新你的 nodejs 安装,你应该会很好。

    这是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
    

    【讨论】:

    • 我将 Node 升级到了 15.7.0 版本,并且可以正常工作。谢谢!
    猜你喜欢
    • 2018-10-30
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    相关资源
    最近更新 更多