AI Workshop: learn to build apps with AI →
Built-in Modules: The Node fs module

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


The fs module provides many useful methods to access and interact with the file system.

There is no need to install it. Being part of the Node core, it can be used by requiring it:

const fs = require('fs')

Once you do so, you have access to all its methods, which include:

  • fs.access(): check if a file exists and whether Node can access it with the current permissions
  • fs.appendFile(): append data to a file. If the file does not exist, it’s created
  • fs.chmod(): change the permissions of a file specified by the filename passed. Related: fs.lchmod(), fs.fchmod()
  • fs.chown(): change the owner and group of a file specified by the filename passed. Related: fs.fchown(), fs.lchown()
  • fs.close(): close a file descriptor
  • fs.copyFile(): copies a file
  • fs.createReadStream(): create a readable file stream
  • fs.createWriteStream(): create a writable file stream
  • fs.link(): create a new hard link to a file
  • fs.mkdir(): create a new folder
  • fs.mkdtemp(): create a temporary directory
  • fs.open(): open a file (optionally set file mode)
  • fs.readdir(): read the contents of a directory
  • fs.readFile(): read the contents of a file. Related: fs.read()
  • fs.readlink(): read the value of a symbolic link
  • fs.realpath(): resolve relative file path pointers (., ..) to the full path
  • fs.rename(): rename a file or folder
  • fs.rmdir(): remove a folder
  • fs.stat(): returns the status of the file identified by the filename passed. Related: fs.fstat(), fs.lstat()
  • fs.symlink(): create a new symbolic link to a file
  • fs.truncate(): truncate to the specified length the file identified by the filename passed. Related: fs.ftruncate()
  • fs.unlink(): remove a file or a symbolic link
  • fs.unwatchFile(): stop watching for changes on a file
  • fs.utimes(): change the timestamp of the file identified by the filename passed. Related: fs.futimes()
  • fs.watchFile(): start watching for changes on a file. Related: fs.watch()
  • fs.writeFile(): write data to a file. Related: fs.write()

All fs methods are asynchronous by default. Synchronous versions are available with a Sync suffix.

For example:

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

This affects how your application runs (async does not block; sync blocks the event loop).

Node.js 10 added experimental support for a promise-based API.

For example, fs.rename() can be used asynchronously with a callback:

const fs = require('fs')

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

  // done
})

A synchronous API can be used like this, with a try/catch block to handle errors:

const fs = require('fs')

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

The key difference here is that the execution of your script will block in the second example, until the file operation completes.

Lessons in this unit:

0: Introduction
1: The Node events module
2: ▶︎ The Node fs module
3: The Node http module
4: The Node os module
5: The Node path module