The Wayback Machine - https://web.archive.org/web/20240913194802/https://www.geeksforgeeks.org/node-js-fs-dir-readsync-method/
Open In App

Node.js fs.Dir.readSync() Method

Last Updated : 06 Feb, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The fs.Dir.readSync() method is an inbuilt application programming interface of class fs.Dir within File System module which is used to read each next dirent of directory.
Syntax: 
 

const dir.readSync()

Parameter: This method does not accept any parameter.
Return Value: This method returns the dirent of the directory.
Below programs illustrates the use of fs.Dir.readSync() method in Node.js:
Example 1: 
Filename: index.js 
 

javascript




// Node.js program to demonstrate the
// dir.readSync() method
const fs = require('fs');
 
// Initiating async function
async function stop(path) {
 
  // Creating and initiating directory's
  // underlying resource handle
  const dir = await fs.promises.opendir(path);
   
  // Synchronously reading the directory's
  // underlying resource handle
  const dirent = dir.readSync();
 
  // Display the result
  console.log(dirent.name);
}
 
// Catching error
stop('./').catch(console.error);


Run index.js file using the following command: 

node index.js

Output: 

abcd.cer

Example 2: 
Filename: index.js 
 

javascript




// Node.js program to demonstrate the
// dir.readSync() method
const fs = require('fs');
   
// Initiating async function
async function stop(path) {
  
  let dir = null;
  
  try {
 
  // Creating and initiating directory's
  // underlying resource handle
  dir = await fs.promises.opendir(
      new URL('file:///F:/java/'));
 
  // Synchronously reading the directory's
  // underlying resource handle
  // using readSync() method
  for(var i = 0 ; i <= 3 ; i ++ ) {
    var dirent = dir.readSync();
 
    // Display the result
    console.log(dirent.name);
  }
 
  } finally {
  
    if (dir) {
 
      // Display the result
      console.log("dir is closed successfully");
 
      // Synchronously closeSyncing the
      // directory's underlying resource
      // handle
      const promise = dir.closeSync()
    }
  }
}
   
// Catching error
stop('./').catch(console.error);


Run index.js file using the following command: 

node index.js

Output: 

abcd.cer
cert.cer
certfile.cer
certificate1.cer
dir is closed successfully

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_dir_readsync
 



Previous Article
Next Article

Similar Reads

Node.js console.dir() Method
The console.dir() method is used to get the list of object properties of a specified object. These object properties also have child objects, from which you can inspect for further information. Syntax: console.dir( object ) Parameters: This method accepts single parameter which holds the object element. Example 1: &lt;!DOCTYPE html&gt; &lt;html&gt;
1 min read
Node.js fs.Dir.read() Method
The fs.Dir.read() method is an inbuilt application programming interface of class fs.Dir within File System module which is used to read each next directory (dirent) one by one asynchronously. Syntax: const fs.Dir.read(callback) Parameter: This method takes a callback function as a parameter that has the following arguments. err: if any error occur
2 min read
Node.js fs.Dir.close() Method
The fs.Dir.close() method is an inbuilt application programming interface of class fs.Dir with in File System module which is used to close the directory's underlying resource handle asynchronously. Syntax: const dir.close() Parameter: This method does not accept any parameter.Return Value: This method returns the promise which is just an enhanceme
2 min read
Node.js fs.Dir.closeSync() Method
The fs.Dir.closeSync() method is an inbuilt application programming interface of class fs.Dir within File System module which is used to close the directory's underlying resource handle synchronously. Syntax: const dir.closeSync() Parameter: This method does not accept any parameter.Return Value: This method returns an undefined object Below progra
2 min read
Node.js fs.Dir.path() Method
The fs.Dir.path() method is an inbuilt application programming interface of class fs.Dir within the File System module which is used to get the path of the directory. Syntax: const dir.path Parameter: This method does not accept any parameter. Return Value: This method returns the path of the directory. Example 1: The below programs illustrate the
2 min read
PHP | dir() (Get instance of the Directory)
The dir() function in PHP used to find the instance of a Directory class. This function read directory, which includes the following: The given directory is opened. The two properties handle and path of dir() are available. The handle property can be used with other directory functions such as readdir(), rewinddir(), closedir(). The path property i
2 min read
PHP | dir() Function
The dir() function in PHP is an inbuilt function which is used to return an instance of the Directory class. The dir() function is used to read a directory, which includes the following: The given directory is opened. The two properties handle and path of dir() are available. Both handle and path properties have three methods: read(), rewind(), and
2 min read
HTML DOM dir Property
The DOM dir Property is used to set or return the value of a dir attribute in an Element. It defines the direction of the text in an Element Content. Syntax HTMLElementObject.dir Return Values: It returns the direction of the text. HTMLElementObject.dir = "ltr|rtl|auto" Properties: ltr: It is a default value and is used to return the text-direction
2 min read
HTML | DOM Bdo dir Property
The Bdo dir Property in HTML DOM is used to set or return the value of the text direction of an element. The dir attribute defines the text direction of an element. Syntax: It is used to return the text direction property. bdoObject.dir It is used to set the text direction property. bdoObject.dir = "ltr|rtl" Property Values: ltr: It defines the lef
2 min read
HTML | &lt;dir&gt; compact Attribute
The HTML &lt;dir&gt; compact Attribute is used to define the list should be smaller than the normal by reducing the space between the list items and the indentation of the list. It is a Boolean attribute.Syntax: &lt;dir compact&gt; Note: The HTML &lt;dir&gt; compact attribute is not supported by HTML 5 instead of using this we can use CSS line-heig
1 min read
HTML | &lt;bdo&gt; dir Attribute
The HTML &lt;bdo&gt; dir attribute is used to define the text direction of a &lt;bdo&gt; element. Syntax &lt;bdo dir="ltr|rtl"&gt; Attribute Values: ltr: It defines the text direction from left-to-right. rtl: It defines the text-direction from right-to-left. Example: &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;HTML bdo dir Attribut
1 min read
Difference between console.dir and console.log
Console Object: The Console object provides access to the browser's debugging console, which can be seen using F12 or ctrl+shift+j. Console object contains many methods, log() and dir() are most used among. The console.log() method prints out a toString representation of the object in the console to the user. Syntax: console.log(object) or console.
2 min read
CSS :dir Selector
:dir is used to select and change the properties of the text element having direction left to right or right to left. Syntax: :dir(ltr|rtl) Parameters: It accepts the following two parameters. ltr: This keyword basically looks for elements having direction from left to right.rtl: This keyword basically looks for elements having direction from right
2 min read
HTML5 MathML dir Attribute
This attributes holds the direction value. It holds two types of direction values ltr for left to right and rtl for right to left. This attribute is accepted by &lt;math&gt;, &lt;mi&gt;, &lt;mo&gt;, &lt;mrow&gt;, &lt;ms&gt;, and &lt;mtext&gt;. Syntax: &lt;element dir="ltr|rtl"&gt; Attribute Values: ltr: This value holds the direction from left to r
1 min read
What is dir Attribute in HTML ?
This is an HTML attribute that is used to specify the text direction of the element content. HTML dir attribute is a global attribute that means it can be applied on any HTML tags for the direction of the element. Syntax: &lt;element dir = "ltr | rtl | auto"&gt; Attribute Value: This attribute contains three values which are listed below. ltr: It i
1 min read
HTML &lt;dir&gt; Tag
The HTML &lt;dir&gt; tag is used to make a list of directory titles. It is not supported in HTML4 as in HTML5 &lt;ul&gt; or CSS are used instead of &lt;dir&gt; tag. The &lt;dir&gt; tag is deprecated &amp; no longer supported in HTML5 Syntax&lt;dir&gt; Lists... &lt;/dir&gt;ValuesValues Descriptions ltr Used for languages that read from left to right
3 min read
HTML dir Attribute
The HTML dir attribute specifies the text directionality, allowing content to be displayed from left-to-right (ltr), right-to-left (rtl), or determined automatically (`auto`) by the browser based on content. Supported Tags: It supports all HTML elements.  Syntax&lt;element dir = "ltr | rtl | auto"&gt;Attribute ValueThis attribute contains three val
2 min read
Node.js Http2ServerRequest.method Method
The Http2ServerRequest.method is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the string representation of the request method. Syntax: const request.method Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the string represe
3 min read
Node.js http.IncomingMessage.method Method
The http.IncomingMessage.method is an inbuilt application programming interface of class Incoming Message within the inbuilt http module which is used to get the type of request method as a string. Syntax: request.method Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the request type name as a
2 min read
Node.js Automatic restart Node.js server with nodemon
We generally type following command for starting NodeJs server: node server.js In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again. node server.js It is a very hectic task for the development process. Nodemon is a package for handling this rest
1 min read
Top 3 Best Packages Of Node.js that you should try being a Node.js Developer
Node.js is an open-source and server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). Node.js has its own package manager called NPM( Node Package Manager) which has very useful and incredible libraries and frameworks that makes our life easier as a developer to work with Node.js. The 3 Best Packages of Node.js that you should
4 min read
Node.js Connect Mysql with Node app
Node.js is a powerful platform for building server-side applications, and MySQL is a widely used relational database. Connecting these two can enable developers to build robust, data-driven applications. In this article, we'll explore how to connect a Node.js application with a MySQL database, covering the necessary setup, configuration, and basic
2 min read
How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
Encountering the error message ‘node’ is not recognized as an internal or external command after installing Node.js can be frustrating, especially when you’re eager to start working on your project. This error usually means that your system cannot find the Node.js executable because the path to it is not set correctly in your system's environment v
2 min read
Javascript Program For Swapping Kth Node From Beginning With Kth Node From End In A Linked List
Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This requirement may be logical in many situations where the linked list data part is huge (For example student details line Name, RollNo, Address, ..etc). The pointers are always fixed (4 bytes for most
5 min read
Javascript Program For Inserting A Node After The N-th Node From The End
Insert a node x after the nth node from the end in the given singly linked list. It is guaranteed that the list contains the nth node from the end. Also 1 &lt;= n. Examples: Input : list: 1-&gt;3-&gt;4-&gt;5 n = 4, x = 2 Output : 1-&gt;2-&gt;3-&gt;4-&gt;5 4th node from the end is 1 and insertion has been done after this node. Input : list: 10-&gt;8
5 min read
Node.js URLSearchParams.has() Method
In URLSearchParams interface, the has() method returns a Boolean which tells us that if the parameter with input name exists it will return true, else false. Syntax: var Bool = URLSearchParams.has(name) Returns: True - if name present, else it will return False. Parameters: name - Input the name of the parameter. Example1: let url = new URL('https:
1 min read
Node.js hmac.update() Method
The hmac.update() method is an inbuilt method of class HMAC within the crypto module which is used to update the data of hmac object. Syntax: hmac.update(data[, inputEncoding])Parameters: This method takes the following two parameters: data: It can be of string, Buffer, TypedArray, or DataView type. It is the data that is passed to this function.in
2 min read
Node.js process.send() Method
The process.send() method is an inbuilt application programming interface of the process module which is used by the child process to communicate with the parent process. This method does not work for the root process because it does not have any parent process. Syntax: process.send(message, [sendHandle])Parameters: This method accepts the followin
2 min read
Node.js writeStream.clearLine() Method
The writeStream.clearLine() is an inbuilt application programming interface of class WriteStream within the module which is used to clear the current line of this write stream object. Syntax: writeStream.clearLine(dir[, callback]) Parameters: This method takes the following argument as a parameter: dir: Integer value defining the selection of a lin
2 min read
Node.js net.SocketAddress() Method
Node.js Net module allows you to create TCP or IPC servers and clients. A TCP client initiates a connection request to a TCP server to establish a connection with the server. Syntax: Below is the syntax for importing the Net module into your node js project: const &lt;Variable_Name&gt; = require('node:net'); const Net_Module = require('node:net');
3 min read