我正在编写一个 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_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;
}
Node 版本:12.6.0
Electron 版本:7.1.12
我无法理解导致此错误的原因以及如何修复它。我知道路径是正确的并且我可以访问目标目录(因为我还使用 fs 模块从该目录读取文件)。
感谢您的帮助。
node版本中添加了opendirSync
方法:v12.12.0
,您必须升级node版本。
历史:
Version Changes
v13.1.0 The bufferSize option was introduced.
v12.12.0 Added in: v12.12.0
您可以阅读有关此的更多信息 here
我是一名优秀的程序员,十分优秀!