TypeError:opendirSync不是函数 [英] TypeError: opendirSync is not a function
问题描述
我正在编写电子应用程序,并且我想使用fs模块的opendirSync函数来计算目录中的文件数。但是,出现以下错误:
(node:12944)UnhandledPromiseRejectionWarning:TypeError:fs.opendirSync不是函数
在DataImporter.CountFiles(C:\Users\v\Documents\Projects\Electron\asts\server\data_importer.js:18:8)
在新DataImporter(C: \用户\v\文档\项目\电子\服务器\data_importer.js:5:26)C:\用户\v\文档\项目
\Electron\asts\main.js:32:19
(节点:12944)UnhandledPromiseRejectionWarning:TypeError:fs.opendir不是DataImporter.CountFiles处的函数
(C:\Users\ \v\Documents\Projects\Electron\asts.server\data_importer.js:18:8)
在新的DataImporter(C:\Users\v\Documents\Projects\ \Eastron\asts\server\data_importer.js:5:26)
在C:\Users\v\Documents\Projects\Electron\asts\main.js:32 :19
(节点:12944)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。该错误是由于在没有catch块的情况下抛出异步函数而引起的,或者是由于拒绝了未经.catch()处理的诺言而引起的。 (拒绝ID:1)
(节点:12944)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。该错误是由于在没有catch块的情况下抛出异步函数而引起的,或者是由于拒绝了.catch()未处理的承诺而导致的。 (拒绝ID:1)
(节点:12944)[DEP0018] DeprecationWarning:已弃用未处理的诺言拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。
(节点:12944)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。
我使用的代码如下:
let fs = require('fs');
let path_mod = require('path');
函数CountFiles(path){
let dir_end = null;
让计数= 0;
let directory = fs.opendirSync(path);
while(1){
let ret = directory.readSync();
if(!ret){
中断;
} else if(ret.isDirectory()){
console.log(path_mod.join(path,ret.name));
count + = CountFiles(path_mod.join(path,ret.name));
} else {
count ++;
}
}
directory.closeSync();
返回计数;
}
节点版本: 12.6.0
电子版本: 7.1.12
<我不明白是什么原因导致了该错误以及如何解决。我知道该路径是正确的,并且可以访问目标目录(因为我还使用了fs模块从该目录中读取文件)。
谢谢
在节点版本中添加了 opendirSync
方法: v12.12.0
,您必须升级节点版本。
历史记录:
版本更改
v13.1.0引入了bufferSize选项。
v12.12.0添加到:v12.12.0
您可以在此处
了解更多信息
I am writing an electron application and I want to use the opendirSync function of the fs module in order to count the number of files in a directory. However, I get the following error:
(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.
The code I am using is as follows:
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;
}
Node Version: 12.6.0
Electron Version: 7.1.12
I cannot understand what causes this error and how to fix it. I know that the path is correct and that I have access to the target directory (since I also used the fs module to read a file from that directory).
Thank you for you help.
The opendirSync
method was added in node version: v12.12.0
, You have to upgrade the node version.
History:
Version Changes
v13.1.0 The bufferSize option was introduced.
v12.12.0 Added in: v12.12.0
You can read more about this here
这篇关于TypeError:opendirSync不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!