/

The Node fs Module: Exploring File System Functionality

The Node fs Module: Exploring File System Functionality

The fs module in Node.js offers a range of valuable functions to interact with the file system efficiently.

To begin using the fs module, you don’t need to install anything. Since it’s a part of the Node core, you can simply require it using the following code snippet:

1
const fs = require('fs')

Once you have required the module, you gain access to a wide array of methods, including:

  • fs.access(): Checks if a file exists and if Node has the necessary permissions to access it.
  • fs.appendFile(): Appends data to a file, creating the file if it doesn’t already exist.
  • fs.chmod(): Alters the permissions of a specified file using the provided filename. Related: fs.lchmod(), fs.fchmod()
  • fs.chown(): Modifies the owner and group details of the specified file using the given filename. Related: fs.fchown(), fs.lchown()
  • fs.close(): Closes a file descriptor.
  • fs.copyFile(): Copies a file.
  • fs.createReadStream(): Establishes a readable file stream.
  • fs.createWriteStream(): Sets up a writable file stream.
  • fs.link(): Creates a new hard link to a file.
  • fs.mkdir(): Generates a new folder.
  • fs.mkdtemp(): Creates a temporary directory.
  • fs.open(): Sets the file mode.
  • fs.readdir(): Reads the contents of a directory.
  • fs.readFile(): Reads the content of a file. Related: fs.read()
  • fs.readlink(): Retrieves the value of a symbolic link.
  • fs.realpath(): Resolves relative file path pointers (such as . and ..) to the full path.
  • fs.rename(): Renames a file or folder.
  • fs.rmdir(): Removes a folder.
  • fs.stat(): Retrieves the status of the file specified by the given filename. Related: fs.fstat(), fs.lstat()
  • fs.symlink(): Establishes a new symbolic link to a file.
  • fs.truncate(): Truncates the file identified by the given filename to the specified length. Related: fs.ftruncate()
  • fs.unlink(): Removes a file or symbolic link.
  • fs.unwatchFile(): Ceases watching for changes on a file.
  • fs.utimes(): Adjusts the timestamp of the file specified by the given filename. Related: fs.futimes()
  • fs.watchFile(): Begins watching for changes on a file. Related: fs.watch()
  • fs.writeFile(): Writes data to a file. Related: fs.write()

An interesting aspect of the fs module is that all its methods are asynchronous by default, but they can also function synchronously by appending Sync to their names.

For instance:

  • fs.rename()
  • fs.renameSync()
  • fs.write()
  • fs.writeSync()

This distinction significantly impacts your application flow.

Note that Node 10 introduces experimental support for a promise-based API as well. To learn more, visit here.

To illustrate the usage of the fs.rename() method, let’s consider the asynchronous API with a callback:

1
2
3
4
5
6
7
8
9
const fs = require('fs')

fs.rename('before.json', 'after.json', (err) => {
if (err) {
return console.error(err)
}

// Success
})

Alternatively, you can employ the synchronous API using a try/catch block to handle errors:

1
2
3
4
5
6
7
8
const fs = require('fs')

try {
fs.renameSync('before.json', 'after.json')
// Success
} catch (err) {
console.error(err)
}

The crucial distinction here is that the execution of your script will be blocked in the second example until the file operation is successful.

Tags: Node.js, file system, fs module