DEV Community

Isaac Tonyloi
Isaac Tonyloi

Posted on

Common fs module functions in Node js

The fs module in Node.js provides file system-related functionality, allowing you to interact with the file system on your computer. Here are some common functions provided by the fs module

filehandle.appendFile(data[, options]) - This function is used to append data to an existing file. It takes two arguments: the data to be appended and an optional options object.

filehandle.chmod(mode) - This function is used to change the file mode of a file. It takes one argument: the new file mode.

filehandle.chown(uid, gid) - This function is used to change the owner and group of a file. It takes two arguments: the new owner user ID (uid) and the new group ID (gid).

filehandle.close() - This function is used to close a file.

filehandle.createReadStream([options]) - This function is used to create a readable stream from a file. It takes an optional options object.

filehandle.createWriteStream([options]) - This function is used to create a writable stream to a file. It takes an optional options object.

filehandle.read(buffer, offset, length, position) - This function is used to read data from a file into a buffer. It takes four arguments: the buffer to read into, the offset to start writing at, the number of bytes to read, and the position to start reading from.

filehandle.readFile(options) - This function is used to read the entire contents of a file. It takes an optional options object.

filehandle.stat([options]) - This function is used to get the status of a file. It takes an optional options object.

filehandle.write(buffer, offset[, length[, position]]) - This function is used to write data from a buffer to a file. It takes four arguments: the buffer to write from, the offset to start reading from, the number of bytes to write, and the position to start writing at.

filehandle.writeFile(data, options) - This function is used to write data to a file. It takes two arguments: the data to write and an optional options object.

Top comments (0)