The fs
module provides a lot of very useful functionality to access and interact with the file system.
There is no need to install it. Being part of the Node.js core, it can be used by simply requiring it:
const fs = require('fs');
Once you do so, you have access to all its methods, which include:
fs.access()
: check if the file exists and Node.js can access it with its permissionsfs.appendFile()
: append data to a file. If the file does not exist, it’s createdfs.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 descriptorfs.copyFile()
: copies a filefs.createReadStream()
: create a readable file streamfs.createWriteStream()
: create a writable file streamfs.link()
: create a new hard link to a filefs.mkdir()
: create a new folderfs.mkdtemp()
: create a temporary directoryfs.open()
: set the file modefs.readdir()
: read the contents of a directoryfs.readFile()
: read the content of a file. Related: fs.read()
fs.readlink()
: read the value of a symbolic linkfs.realpath()
: resolve relative file path pointers (.
, ..
) to the full path