Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: opendirSync is not a function

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.

like image 937
Vag Avatar asked Nov 22 '25 20:11

Vag


1 Answers

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

like image 117
Sohail Ashraf Avatar answered Nov 24 '25 11:11

Sohail Ashraf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!