Javascript TypeError:opendirSync不是函数
我正在编写一个electron应用程序,我想使用fs模块的opendirSync函数来计算目录中的文件数。但是,我得到以下错误:Javascript TypeError:opendirSync不是函数,javascript,node.js,electron,Javascript,Node.js,Electron,我正在编写一个electron应用程序,我想使用fs模块的opendirSync函数来计算目录中的文件数。但是,我得到以下错误: (node:12944) UnhandledPromiseRejectionWarning: TypeError: fs.opendirSync is not a function at DataImporter.CountFiles (C:\Users\v\Documents\Projects\Electron\asts\server\data_import
(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.
我使用的代码如下:
让fs=require('fs');
让path_mod=require('path');
函数CountFiles(路径){
设dir_end=null;
让计数=0;
让directory=fs.opendirSync(路径);
而(1){
让ret=directory.readSync();
如果(!ret){
打破
}else if(ret.isDirectory()){
log(path_mod.join(path,ret.name));
count+=CountFiles(path_mod.join(path,ret.name));
}否则{
计数++;
}
}
closeSync()目录;
返回计数;
}
节点版本:12.6.0
电子版:7.1.12
我无法理解导致此错误的原因以及如何修复它。我知道路径是正确的,并且我可以访问目标目录(因为我还使用fs模块从该目录读取文件)
感谢您的帮助。节点版本中添加了
opendirSync
方法:v12.12.0
,您必须升级节点版本
历史记录:
Version Changes
v13.1.0 The bufferSize option was introduced.
v12.12.0 Added in: v12.12.0
你可以阅读更多关于这方面的内容嗯,由于编辑的自动完成功能,我没有想到这一点。非常感谢,这修正了我的错误。