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 programs illustrates the use of fs.Dir.closeSync() method.
Example 1:
Filename: index.js
// Node.js program to demonstrate the // dir.closeSync() method const fs = require('fs'); // Initiating asyn function async function stop(path) { // Creating and initiating directory's // underlying resource handle const dir = await fs.promises.opendir(path); console.log("All the dirent beofre operation :- "); for (var i = 0; i <= 6; i++) { console.log(((dir.readSync())).name); } // Synchronously closeSyncing the directory's // underlying resource handle const promise = dir.closeSync(); // Display the result console.log("dir is closed successfully"); } // Catching error stop('./').catch(console.error); |
Run index.js file using the following command:
node index.js
Output:
All the dirent beofre operation :- abcd.cer books.txt cert.cer certfile.cer certificate1.cer example.txt features dir is closed successfully
Example 2:
Filename: index.js
// Node.js program to demonstrate the // dir.closeSync() method const fs = require('fs'); const fsPromises = fs.promises; // Name of the directory to be created let directo = './temp'; // Checking if the directory is present or not if (!fs.existsSync(directo)){ fs.mkdirSync(directo); } // Initiating asynchronous function async function funct(path) { // Initializng dir let dir = null; let pathval = null; try { // Creating and initiating directory's // underlying resource handle dir = await fs.promises.opendir( new URL('file:///F:/java/temp')); console.log("All the dirent before operation :- " + ((dir.readSync()))); // Getting the path of the directory // by using path() method pathval = dir.path; } finally { if (dir) { // Close the file if it is opened. console.log("Path :- " + pathval); console.log("All the dirent beofre operation :- " + ((dir.readSync()))); console.log("dir is closed successfully"); await dir.closeSync(); } } } funct('./').catch(console.error); |
Run index.js file using the following command:
node index.js
Output:
All the dirent before operation :- [object Object] Path :- F: All the dirent before operation :- [object Object] dir is closed successfully
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/fs.html#fs_dir_closesync
Recommended Posts:
- Nodejs | Automatic restart NodeJs server with nodemon
- Nodejs console.timeLog() Method
- Nodejs | DNS
- Nodejs | Jimp
- Web-Socket in NodeJS
- Nodejs | DNS | setServers()
- Nodejs | GM write() Function
- Nodejs | GM whitePoint() Function
- Nodejs | GM transparent() Function
- Nodejs | GM drawCircle() Function
- Nodejs | GM whiteThreshold() Function
- Nodejs | GM resize() Function
- Nodejs | GM drawEllipse() Function
- Nodejs | GM sharpen() Function
- Encrypting Data in NodeJS
- Difference between Reactjs and Nodejs
- Nodejs | GM motionBlur() Function
- Nodejs | GM operator() Function
- Nodejs | GM charcoal() Function
- Nodejs | GM blur() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

