Node.js fs.closeSync() Method - GeeksforGeeks

08 Oct, 2021
The fs.closeSync() method is used to synchronously close the given file descriptor thereby clearing the file that is associated with it. It allows the file descriptor to be reused for other files. Calling fs.closeSync() on a file descriptor while some other operation is being performed on it may lead to undefined behavior.
Syntax:
fs.closeSync( fd )
Parameters: This method accepts single parameter as mentioned above and described below:
fd: It is an integer which denotes the file descriptor of the file of which to be closed.
Below examples illustrate the fs.closeSync() method in Node.js:
Example 1: This example shows the closing of a file descriptor.
// Node.js program to demonstrate the// fs.closeSync() method  // Import the filesystem moduleconst fs = require('fs');  // Get the file descriptor of the given pathfile_descriptor = fs.openSync("example.txt");console.log("The file descriptor is:", file_descriptor);  // Close the file descriptortry {  fs.closeSync(file_descriptor);  console.log("\n> File Closed successfully");} catch (err) {  console.error('Failed to close file');}
Output:
The file descriptor is: 3

> File Closed successfully
Example 2: This example shows the closing of a file descriptor and trying to access the closed file descriptor again.
// Node.js program to demonstrate the// fs.closeSync() method  // Import the filesystem moduleconst fs = require('fs');  // Get the file descriptor of the given pathfile_descriptor = fs.openSync("example.txt");console.log("The file descriptor is:", file_descriptor);  // Attempting to get stats before closingconsole.log("\n> Finding the stats of the file");try {  statsObj = fs.fstatSync(file_descriptor);  console.log("Stats of the file generated");} catch (err) {  console.error('Cannot find stats of file', err);}  // Close the file descriptortry {  fs.closeSync(file_descriptor);  console.log("\n> File Closed successfully");} catch (err) {  console.error('Failed to close file', err);}  // Attempting to find stats after closingconsole.log("\n> Finding the stats of the file again");try {  statsObj = fs.fstatSync(file_descriptor);  console.log("Stats of the file generated");} catch (err) {  console.error('Cannot find stats of file', err);}
Output:
The file descriptor is: 3

> Finding the stats of the file
Stats of the file generated

> File Closed successfully

> Finding the stats of the file again
Cannot find stats of file Error: EBADF: bad file descriptor, fstat
    at Object.fstatSync (fs.js:897:3)
    at Object. (G:\tutorials\nodejs-fs-closeSync\index.js:46:17)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11 {
  fd: 3,
  errno: -4083,
  syscall: 'fstat',
  code: 'EBADF'
}
Reference: https://nodejs.org/api/fs.html#fs_fs_closesync_fd
Node.js-fs-module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Express.js express.Router() Function
JWT Authentication with Node.js
Express.js req.params Property
Mongoose Populate() Method
How to build a basic CRUD app with Node.js and ReactJS ?
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
Convert a string to an integer in JavaScript