类型错误:opendirSync 不是函数
Posted
技术标签:
【中文标题】类型错误:opendirSync 不是函数【英文标题】:TypeError: opendirSync is not a function 【发布时间】:2020-05-31 12:03:42 【问题描述】:我正在编写一个电子应用程序,我想使用 fs 模块的 opendirSync 函数来计算目录中的文件数。但是,我收到以下错误:
(node:12944) UnhandledPromiseRejectionWarning: TypeError: fs.opendirSync is not a function
at DataImporter.CountFiles (C:\Users\v\Documents\Projects\Electron\asts\server\data_importer.js:18:8)
at new DataImporter (C:\Users\v\Documents\Projects\Electron\asts\server\data_importer.js:5:26)
at C:\Users\v\Documents\Projects\Electron\asts\main.js:32:19
(node:12944) UnhandledPromiseRejectionWarning: TypeError: fs.opendir is not a function
at DataImporter.CountFiles (C:\Users\v\Documents\Projects\Electron\asts\server\data_importer.js:18:8)
at new DataImporter (C:\Users\v\Documents\Projects\Electron\asts\server\data_importer.js:5:26)
at C:\Users\v\Documents\Projects\Electron\asts\main.js:32:19
(node:12944) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12944) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:12944) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:12944) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我使用的代码如下:
let fs = require('fs');
let path_mod = require('path');
function CountFiles(path)
let dir_end = null;
let count = 0;
let directory = fs.opendirSync(path);
while(1)
let ret = directory.readSync();
if(!ret)
break;
else if(ret.isDirectory())
console.log(path_mod.join(path, ret.name));
count += CountFiles(path_mod.join(path, ret.name));
else
count++;
directory.closeSync();
return count;
节点版本:12.6.0
电子版:7.1.12
我无法理解导致此错误的原因以及如何修复它。我知道路径是正确的并且我可以访问目标目录(因为我还使用 fs 模块从该目录读取文件)。
感谢您的帮助。
【问题讨论】:
【参考方案1】:在节点版本:v12.12.0
中添加了opendirSync
方法,您必须升级节点版本。
历史:
Version Changes
v13.1.0 The bufferSize option was introduced.
v12.12.0 Added in: v12.12.0
你可以阅读更多关于这个here
【讨论】:
呃,由于编辑器的自动完成,我没有想到这一点。非常感谢,这解决了我的错误。以上是关于类型错误:opendirSync 不是函数的主要内容,如果未能解决你的问题,请参考以下文章
XMLHttpRequest().Open 在 Firefox 中不是函数,但在 Chrome 中有效