How to use the opendirSync function from fs

Find comprehensive JavaScript fs.opendirSync code examples handpicked from public code repositorys.

fs.opendirSync is a synchronous method of the fs module in Node.js that opens a directory for reading and returns a directory object.

73
74
75
76
77
78
79
80
81
82
const dest = path.resolve('../docs/demo'),
    src = path.resolve('build'),
    pyodideDest = path.resolve('../docs/demo/pyodide'),
    pyodideSrc = path.resolve('../../pyodide/dist'),
    pyodideExists = fs.existsSync(pyodideSrc),
    d = fs.opendirSync(dest);
let entry;
while ((entry = d.readSync()) !== null) {
    let destFilePath = path.join(dest, entry.name);
    if (entry.name === 'pyodide' && ! pyodideExists) {
fork icon54
star icon268
watch icon0

170
171
172
173
174
175
176
177
178
179
180


function cleanGeneratedFiles(outputDir) {
    if (!fs.existsSync(outputDir)) {
        return;
    }
    const dir = fs.opendirSync(outputDir);
    while (true) {
        const dirEnt = dir.readSync()
        if (dirEnt === null) {
            break;
fork icon32
star icon57
watch icon0

+ 2 other calls in file

How does fs.opendirSync work?

The fs.opendirSync() method is used to synchronously open a directory and returns an instance of the fs.Dir class, which provides methods for reading directory entries. When called, it takes the directory path as an argument and returns an fs.Dir object representing the opened directory.

37
38
39
40
41
42
43
44
45
46
47
48
49
let chunkSize = 19950;


let config = {};
let hashes = {};


const directory = fs.opendirSync(CONFIG_FOLDER)
let file;


while ((file = directory.readSync()) !== null) {
    let device = file.name.split(".")[0];
fork icon2
star icon3
watch icon0

86
87
88
89
90
91
92
93
94
95
96
97
  sync = false;
}));


// opendir() on file should throw ENOTDIR
assert.throws(function() {
  fs.opendirSync(__filename);
}, /Error: ENOTDIR: not a directory/);


assert.throws(function() {
  fs.opendir(__filename);
fork icon0
star icon0
watch icon1

+ 17 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const fs = require("fs");

try {
  const dir = fs.opendirSync("./myDirectory");

  let dirent;
  while ((dirent = dir.readSync()) !== null) {
    console.log(dirent.name);
  }

  dir.closeSync();
} catch (err) {
  console.error(err);
}

In this example, we first use fs.opendirSync to open a directory named "myDirectory". We then loop through each entry in the directory using dir.readSync, which returns a Dirent object representing the next file or directory in the directory, or null if there are no more entries. We log the name of each Dirent to the console. Finally, we call dir.closeSync to close the directory. If an error occurs at any point, we log it to the console.

39
40
41
42
43
44
45
46
47
48
    }

    read();
  });
} else {
  const dir = fs.opendirSync(fullPath, { bufferSize });
  while (dir.readSync() !== null)
    counter++;
  dir.closeSync();
}
fork icon0
star icon0
watch icon0

191
192
193
194
195
196
197
198
199
200
201
doAsyncIterThrowTest().then(common.mustCall());


// Check error thrown on invalid values of bufferSize
for (const bufferSize of [-1, 0, 0.5, 1.5, Infinity, NaN]) {
  assert.throws(
    () => fs.opendirSync(testDir, common.mustNotMutateObjectDeep({ bufferSize })),
    {
      code: 'ERR_OUT_OF_RANGE'
    });
}
fork icon0
star icon0
watch icon0

+ 17 other calls in file

36
37
38
39
40
41
42
43
44
45
mongoose.connection.db.dropCollection('revisions');
mongoose.connection.db.dropCollection('articles');
mongoose.connection.db.dropCollection('yearcounts');
mongoose.connection.db.dropCollection('authors');

const dir = fs.opendirSync(revisionsDir.path)
let dirent;
while ((dirent = dir.readSync()) !== null) {
  let path = revisionsDir.path + '/' + dirent.name;
  batch = JSON.parse(fs.readFileSync(path).toString());
fork icon0
star icon0
watch icon0

57
58
59
60
61
62
63
64
65
66
if (interaction.commandName === "start") {
    if (interaction.member.permissions.has(PermissionFlagsBits.Administrator)) {

      var amount = 0;
      let file
      const directory = fs.opendirSync('./data/' + interaction.guildId)

      while ((file = directory.readSync()) !== null) {
        amount = amount + 1;
      }
fork icon0
star icon0
watch icon0

+ 3 other calls in file