Usage in Deno
import * as mod from "node:fs";
The node:fs module enables interacting with the file system in a
way modeled on standard POSIX functions.
To use the promise-based APIs:
import * as fs from 'node:fs/promises';
To use the callback and sync APIs:
import * as fs from 'node:fs';
All file system operations have synchronous, callback, and promise-based forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).
A representation of a directory entry, which can be a file or a subdirectorywithin the directory, as returned by reading from an fs.Dir. Thedirectory entry is a combination of the file name and file type pairs.
Instances of fs.ReadStream are created and returned using the createReadStream function.
Instances of fs.WriteStream are created and returned using the createWriteStream function.
Tests a user's permissions for the file or directory specified by path.The mode argument is an optional integer that specifies the accessibilitychecks to be performed. mode should be either the value fs.constants.F_OK or a mask consisting of the bitwise OR of any of fs.constants.R_OK, fs.constants.W_OK, and fs.constants.X_OK(e.g.fs.constants.W_OK | fs.constants.R_OK). Check File access constants forpossible values of mode.
Synchronously tests a user's permissions for the file or directory specifiedby path. The mode argument is an optional integer that specifies theaccessibility checks to be performed. mode should be either the value fs.constants.F_OK or a mask consisting of the bitwise OR of any of fs.constants.R_OK, fs.constants.W_OK, andfs.constants.X_OK (e.g.fs.constants.W_OK | fs.constants.R_OK). Check File access constants forpossible values of mode.
Asynchronously append data to a file, creating the file if it does not yetexist. data can be a string or a Buffer.
Synchronously append data to a file, creating the file if it does not yetexist. data can be a string or a Buffer.
Asynchronously changes the permissions of a file. No arguments other than apossible exception are given to the completion callback.
Asynchronously changes owner and group of a file. No arguments other than apossible exception are given to the completion callback.
Closes the file descriptor. No arguments other than a possible exception aregiven to the completion callback.
Closes the file descriptor. Returns undefined.
Asynchronously copies src to dest. By default, dest is overwritten if italready exists. No arguments other than a possible exception are given to thecallback function. Node.js makes no guarantees about the atomicity of the copyoperation. If an error occurs after the destination file has been opened forwriting, Node.js will attempt to remove the destination.
Synchronously copies src to dest. By default, dest is overwritten if italready exists. Returns undefined. Node.js makes no guarantees about theatomicity of the copy operation. If an error occurs after the destination filehas been opened for writing, Node.js will attempt to remove the destination.
Asynchronously copies the entire directory structure from src to dest,including subdirectories and files.
Synchronously copies the entire directory structure from src to dest,including subdirectories and files.
Unlike the 16 KiB default highWaterMark for a stream.Readable, the streamreturned by this method has a default highWaterMark of 64 KiB.
options may also include a start option to allow writing data at someposition past the beginning of the file, allowed values are in the[0, Number.MAX_SAFE_INTEGER] range. Modifying a file rather thanreplacing it may require the flags option to be set to r+ rather than thedefault w. The encoding can be any one of those accepted by Buffer.
Returns true if the path exists, false otherwise.
Sets the permissions on the file. No arguments other than a possible exceptionare given to the completion callback.
Sets the permissions on the file. Returns undefined.
Sets the owner of the file. No arguments other than a possible exception aregiven to the completion callback.
Sets the owner of the file. Returns undefined.
Forces all currently queued I/O operations associated with the file to theoperating system's synchronized I/O completion state. Refer to the POSIX fdatasync(2) documentation for details. No arguments otherthan a possibleexception are given to the completion callback.
Forces all currently queued I/O operations associated with the file to theoperating system's synchronized I/O completion state. Refer to the POSIX fdatasync(2) documentation for details. Returns undefined.
Invokes the callback with the fs.Stats for the file descriptor.
Retrieves the fs.Stats for the file descriptor.
Truncates the file descriptor. No arguments other than a possible exception aregiven to the completion callback.
Truncates the file descriptor. Returns undefined.
Synchronous version of futimes. Returns undefined.
Retrieves the files matching the specified pattern.
Retrieves the files matching the specified pattern.
Set the owner of the symbolic link. No arguments other than a possibleexception are given to the completion callback.
Set the owner for the path. Returns undefined.
Retrieves the fs.Stats for the symbolic link referred to by the path.The callback gets two arguments (err, stats) where stats is a fs.Stats object. lstat() is identical to stat(), except that if path is a symboliclink, then the link itself is stat-ed, not the file that it refers to.
Change the file system timestamps of the symbolic link referenced by path.Returns undefined, or throws an exception when parameters are incorrect orthe operation fails. This is the synchronous version of lutimes.
Asynchronously creates a directory.
Creates a unique temporary directory.
Returns the created directory path.
Returns a Blob whose data is backed by the given file.
Asynchronously open a directory. See the POSIX opendir(3) documentation formore details.
Synchronously open a directory. See opendir(3).
Returns an integer representing the file descriptor.
Tests a user's permissions for the file or directory specified by path.The mode argument is an optional integer that specifies the accessibilitychecks to be performed. mode should be either the value fs.constants.F_OK or a mask consisting of the bitwise OR of any of fs.constants.R_OK, fs.constants.W_OK, and fs.constants.X_OK(e.g.fs.constants.W_OK | fs.constants.R_OK). Check File access constants forpossible values of mode.
Asynchronously append data to a file, creating the file if it does not yetexist. data can be a string or a Buffer.
Changes the permissions of a file.
Changes the ownership of a file.
Asynchronously copies src to dest. By default, dest is overwritten if italready exists.
Asynchronously copies the entire directory structure from src to dest,including subdirectories and files.
Retrieves the files matching the specified pattern.
Changes the ownership on a symbolic link.
Creates a new link from the existingPath to the newPath. See the POSIX link(2) documentation for more detail.
Equivalent to fsPromises.stat() unless path refers to a symbolic link,in which case the link itself is stat-ed, not the file that it refers to.Refer to the POSIX lstat(2) document for more detail.
Changes the access and modification times of a file in the same way as fsPromises.utimes(), with the difference that if the path refers to asymbolic link, then the link is not dereferenced: instead, the timestamps ofthe symbolic link itself are changed.
Asynchronously creates a directory.
Creates a unique temporary directory. A unique directory name is generated byappending six random characters to the end of the provided prefix. Due toplatform inconsistencies, avoid trailing X characters in prefix. Someplatforms, notably the BSDs, can return more than six random characters, andreplace trailing X characters in prefix with random characters.
Opens a FileHandle.
Asynchronously open a directory for iterative scanning. See the POSIX opendir(3) documentation for more detail.
Reads the contents of a directory.
Asynchronously reads the entire contents of a file.
Reads the contents of the symbolic link referred to by path. See the POSIX readlink(2) documentation for more detail. The promise isfulfilled with thelinkString upon success.
Determines the actual location of path using the same semantics as the fs.realpath.native() function.
Renames oldPath to newPath.
Removes files and directories (modeled on the standard POSIX rm utility).
Removes the directory identified by path.
Creates a symbolic link.
Truncates (shortens or extends the length) of the content at path to len bytes.
If path refers to a symbolic link, then the link is removed without affectingthe file or directory to which that link refers. If the path refers to a filepath that is not a symbolic link, the file is deleted. See the POSIX unlink(2) documentation for more detail.
Change the file system timestamps of the object referenced by path.
Returns an async iterator that watches for changes on filename, where filenameis either a file or a directory.
Asynchronously writes data to a file, replacing the file if it already exists. data can be a string, a buffer, anAsyncIterable, or anIterable object.
Read data from the file specified by fd.
Reads the contents of a directory. The callback gets two arguments (err, files) where files is an array of the names of the files in the directory excluding '.' and '..'.
Reads the contents of the directory.
Asynchronously reads the entire contents of a file.
Returns the contents of the path.
Reads the contents of the symbolic link referred to by path. The callback getstwo arguments (err, linkString).
Returns the symbolic link's string value.
Returns the number of bytesRead.
Read from a file specified by fd and write to an array of ArrayBufferViewsusing readv().
Asynchronously computes the canonical pathname by resolving ., .., andsymbolic links.
Asynchronous realpath(3).
Returns the resolved pathname.
Asynchronously rename file at oldPath to the pathname providedas newPath. In the case that newPath already exists, it willbe overwritten. If there is a directory at newPath, an error willbe raised instead. No arguments other than a possible exception aregiven to the completion callback.
Renames the file from oldPath to newPath. Returns undefined.
Asynchronously removes files and directories (modeled on the standard POSIX rm utility). No arguments other than a possible exception are given to thecompletion callback.
Synchronously removes files and directories (modeled on the standard POSIX rm utility). Returns undefined.
Synchronous statfs(2). Returns information about the mounted file system whichcontains path.
Creates the link called path pointing to target. No arguments other than apossible exception are given to the completion callback.
Returns undefined.
Truncates the file. No arguments other than a possible exception aregiven to the completion callback. A file descriptor can also be passed as thefirst argument. In this case, fs.ftruncate() is called.
Truncates the file. Returns undefined. A file descriptor can also bepassed as the first argument. In this case, fs.ftruncateSync() is called.
Asynchronously removes a file or symbolic link. No arguments other than apossible exception are given to the completion callback.
Synchronous unlink(2). Returns undefined.
Stop watching for changes on filename. If listener is specified, only thatparticular listener is removed. Otherwise, all listeners are removed,effectively stopping watching of filename.
Change the file system timestamps of the object referenced by path.
Returns undefined.
Watch for changes on filename, where filename is either a file or adirectory.
Watch for changes on filename. The callback listener will be called eachtime the file is accessed.
Write buffer to the file specified by fd.
When file is a filename, asynchronously writes data to the file, replacing thefile if it already exists. data can be a string or a buffer.
Returns undefined.
Write an array of ArrayBufferViews to the file specified by fd using writev().
For detailed information, see the documentation of the asynchronous version ofthis API: writev.
Test whether or not the given path exists by checking with the file system.Then call the callback argument with either true or false:
Changes the permissions on a symbolic link. No arguments other than a possibleexception are given to the completion callback.
Changes the permissions on a symbolic link. Returns undefined.
Changes the permissions on a symbolic link.
A fs.Stats object provides information about a file.
Provides information about a mounted file system.
Watch for changes on filename. The callback listener will be called eachtime the file is accessed.
Valid types for path values in "fs".
Constant for fs.copyFile. Flag indicating the destination file should not be overwritten if it already exists.
Constant for fs.copyFile. copy operation will attempt to create a copy-on-write reflink.If the underlying platform does not support copy-on-write, then a fallback copy mechanism is used.
Constant for fs.copyFile. Copy operation will attempt to create a copy-on-write reflink.If the underlying platform does not support copy-on-write, then the operation will fail with an error.
Constant for fs.access(). File is visible to the calling process.
Constant for fs.open(). Flag indicating that data will be appended to the end of the file.
Constant for fs.open(). Flag indicating to create the file if it does not already exist.
Constant for fs.open(). When set, an attempt will be made to minimize caching effects of file I/O.
Constant for fs.open(). Flag indicating that the open should fail if the path is not a directory.
Constant for fs.open(). Flag indicating that the file is opened for synchronous I/O with write operations waiting for data integrity.
Constant for fs.open(). Flag indicating that opening a file should fail if the O_CREAT flag is set and the file already exists.
constant for fs.open().Flag indicating reading accesses to the file system will no longer result inan update to the atime information associated with the file.This flag is available on Linux operating systems only.
Constant for fs.open(). Flag indicating that if path identifies a terminal device,opening the path shall not cause that terminal to become the controlling terminal for the process(if the process does not already have one).
Constant for fs.open(). Flag indicating that the open should fail if the path is a symbolic link.
Constant for fs.open(). Flag indicating to open the file in nonblocking mode when possible.
Constant for fs.open(). Flag indicating to open a file for read-only access.
Constant for fs.open(). Flag indicating to open a file for read-write access.
Constant for fs.open(). Flag indicating to open the symbolic link itself rather than the resource it is pointing to.
Constant for fs.open(). Flag indicating that the file is opened for synchronous I/O.
Constant for fs.open(). Flag indicating that if the file exists and is a regular file, and the file is opened successfully for write access, its length shall be truncated to zero.
Constant for fs.open(). Flag indicating to open a file for write-only access.
Constant for fs.access(). File can be read by the calling process.
Constant for fs.Stats mode property for determining a file's type. File type constant for a block-oriented device file.
Constant for fs.Stats mode property for determining a file's type. File type constant for a character-oriented device file.
Constant for fs.Stats mode property for determining a file's type. File type constant for a directory.
Constant for fs.Stats mode property for determining a file's type. File type constant for a FIFO/pipe.
Constant for fs.Stats mode property for determining a file's type. File type constant for a symbolic link.
Constant for fs.Stats mode property for determining a file's type. Bit mask used to extract the file type code.
Constant for fs.Stats mode property for determining a file's type. File type constant for a regular file.
Constant for fs.Stats mode property for determining a file's type. File type constant for a socket.
Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable by group.
Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable by others.
Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable by owner.
Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable, writable and executable by group.
Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable, writable and executable by others.
Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable, writable and executable by owner.
Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating writable by group.
Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating writable by others.
Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating writable by owner.
Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by group.
Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by others.
Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by owner.
When set, a memory file mapping is used to access the file. This flagis available on Windows operating systems only. On other operating systems,this flag is ignored.
Constant for fs.access(). File can be written by the calling process.
Constant for fs.access(). File can be executed by the calling process.
Synchronous lstat(2) - Get file status. Does not dereference symbolic links.
Synchronous stat(2) - Get file status.