Ошибка типа: opendirSyn c не является функцией - PullRequest
1 голос
/ 16 февраля 2020

Я пишу электронное приложение и хочу использовать функцию opendirSyn c модуля fs для подсчета количества файлов в каталоге. Однако я получаю следующую ошибку:

(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 Ответ

2 голосов
/ 16 февраля 2020

В версию узла добавлен метод opendirSync: v12.12.0, Вам необходимо обновить версию узла.

История:

Version Changes
v13.1.0  The bufferSize option was introduced.

v12.12.0 Added in: v12.12.0 

Вы можете узнать больше об этом здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...