Node.js module
fs
The 'node:fs' module provides file system I/O operations, including reading, writing, renaming, and deleting files and directories. It offers both synchronous and asynchronous methods.
Fully supported in Bun
Fully implemented. 92% of Node.js's test suite passes.
Functions 88
- fs.access()
Tests a user's permissions for the file or directory specified by
path. - fs.accessSync()
Synchronously tests a user's permissions for the file or directory specified by
path. - fs.appendFile()
Asynchronously append data to a file, creating the file if it does not yet exist.
- fs.appendFileSync()
Synchronously append data to a file, creating the file if it does not yet exist.
- fs.chmod()
Asynchronously changes the permissions of a file.
- fs.chmodSync()
For detailed information, see the documentation of the asynchronous version of this API: chmod.
- fs.chown()
Asynchronously changes owner and group of a file.
- fs.chownSync()
Synchronously changes owner and group of a file.
- fs.close()
Closes the file descriptor.
- fs.closeSync()
Closes the file descriptor.
- fs.copyFile()
Asynchronously copies
srctodest. - fs.copyFileSync()
Synchronously copies
srctodest. - fs.cp()
Asynchronously copies the entire directory structure from
srctodest, including subdirectories and files. - fs.cpSync()
Synchronously copies the entire directory structure from
srctodest, including subdirectories and files. - fs.createReadStream()
optionscan includestartandendvalues to read a range of bytes from the file instead of the entire file. - fs.createWriteStream()
optionsmay also include astartoption to allow writing data at some position past the beginning of the file, allowed values are in the \0, [`Number.MAX_SAFE_INTE… - fs.fchmod()
Sets the permissions on the file.
- fs.fchmodSync()
Sets the permissions on the file.
- fs.fchown()
Sets the owner of the file.
- fs.fchownSync()
Sets the owner of the file.
- fs.fdatasync()
Forces all currently queued I/O operations associated with the file to the operating system's synchronized I/O completion state.
- fs.fdatasyncSync()
Forces all currently queued I/O operations associated with the file to the operating system's synchronized I/O completion state.
- fs.fstat()
Invokes the callback with the
fs.Statsfor the file descriptor. - fs.fstatSync()
Retrieves the
fs.Statsfor the file descriptor. - fs.fsync()
Request that all data for the open file descriptor is flushed to the storage device.
- fs.fsyncSync()
Request that all data for the open file descriptor is flushed to the storage device.
- fs.ftruncate()
Truncates the file descriptor.
- fs.ftruncateSync()
Truncates the file descriptor.
- fs.futimes()
Change the file system timestamps of the object referenced by the supplied file descriptor.
- fs.futimesSync()
Synchronous version of futimes.
- fs.glob()
Retrieves the files matching the specified pattern.
- fs.globSync()
When
followSymlinksis enabled, detected symbolic link cycles are not traversed recursively. - fs.lchown()
Set the owner of the symbolic link.
- fs.lchownSync()
Set the owner for the path.
- fs.link()
Creates a new link from the
existingPathto thenewPath. - fs.linkSync()
Creates a new link from the
existingPathto thenewPath. - fs.lstat()
Retrieves the
fs.Statsfor the symbolic link referred to by the path. - fs.lstatSync()
Synchronous lstat(2) - Get file status.
- fs.lutimes()
Changes the access and modification times of a file in the same way as utimes, with the difference that if the path refers to a symbolic link, then the link is not der…
- fs.lutimesSync()
Change the file system timestamps of the symbolic link referenced by
path. - fs.mkdir()
Asynchronously creates a directory.
- fs.mkdirSync()
Synchronously creates a directory.
- fs.mkdtemp()
Creates a unique temporary directory.
- fs.mkdtempDisposableSync()
Returns a disposable object whose
pathproperty holds the created directory path. - fs.mkdtempSync()
Returns the created directory path.
- fs.open()
Asynchronous file open.
- fs.openAsBlob()
Returns a
Blobwhose data is backed by the given file. - fs.opendir()
Asynchronously open a directory.
- fs.opendirSync()
Synchronously open a directory.
- fs.openSync()
Returns an integer representing the file descriptor.
- fs.read()
Read data from the file specified by
fd. - fs.readdir()
Reads the contents of a directory.
- fs.readdirSync()
Reads the contents of the directory.
- fs.readFile()
Asynchronously reads the entire contents of a file.
- fs.readFileSync()
Returns the contents of the
path. - fs.readlink()
Reads the contents of the symbolic link referred to by
path. - fs.readlinkSync()
Returns the symbolic link's string value.
- fs.readSync()
Returns the number of
bytesRead. - fs.readv()
Read from a file specified by
fdand write to an array ofArrayBufferViews usingreadv(). - fs.readvSync()
For detailed information, see the documentation of the asynchronous version of this API: readv.
- fs.realpath()
Asynchronously computes the canonical pathname by resolving
.,.., and symbolic links. - fs.realpathSync()
Returns the resolved pathname.
- fs.rename()
Asynchronously rename file at
oldPathto the pathname provided asnewPath. - fs.renameSync()
Renames the file from
oldPathtonewPath. - fs.rm()
Asynchronously removes files and directories (modeled on the standard POSIX
rmutility). - fs.rmdir()
Asynchronous
rmdir(2). - fs.rmdirSync()
Synchronous
rmdir(2). - fs.rmSync()
Synchronously removes files and directories (modeled on the standard POSIX
rmutility). - fs.stat()
Asynchronous
stat(2). - fs.statfs()
Asynchronous
statfs(2). - fs.statfsSync()
Synchronous
statfs(2). - fs.statSync()
Synchronous stat(2) - Get file status.
- fs.symlink()
Creates the link called
pathpointing totarget. - fs.symlinkSync()
Returns
undefined. - fs.truncate()
Asynchronous truncate(2) - Truncate a file to a specified length.
- fs.unlink()
Asynchronously removes a file or symbolic link.
- fs.unlinkSync()
Synchronous
unlink(2). - fs.unwatchFile()
Stop watching for changes on
filename. - fs.utimes()
Change the file system timestamps of the object referenced by
path. - fs.utimesSync()
Returns
undefined. - fs.watch()
Watch for changes on
filename, wherefilenameis either a file or a directory. - fs.watchFile()
Watch for changes on
filename. - fs.write()
Write
bufferto the file specified byfd. - fs.writeFile()
When
fileis a filename, asynchronously writes data to the file, replacing the file if it already exists. - fs.writeFileSync()
Returns
undefined. - fs.writeSync()
For detailed information, see the documentation of the asynchronous version of this API: write.
- fs.writev()
Write an array of
ArrayBufferViews to the file specified byfdusingwritev(). - fs.writevSync()
For detailed information, see the documentation of the asynchronous version of this API: writev.
Classes 7
- class fs.Dir
A class representing a directory stream.
- class fs.Dirent
A representation of a directory entry, which can be a file or a subdirectory within the directory, as returned by reading from an
fs.Dir. - class fs.ReadStream
Instances of
fs.ReadStreamcannot be constructed directly. - class fs.Stats
A
fs.Statsobject provides information about a file. - class fs.StatsFs
Provides information about a mounted file system.
- class fs.Utf8Stream
An optimized UTF-8 stream writer that allows for flushing all the internal buffering on demand.
- class fs.WriteStream
Instances of
fs.WriteStreamcannot be constructed directly.
Interfaces 40
- interface BigIntOptions
- interface BigIntStats
- interface BigIntStatsFs
- interface CopyOptions
- interface CopyOptionsBase
- interface CopySyncOptions
- interface CreateReadStreamFSImplementation
- interface CreateWriteStreamFSImplementation
- interface DisposableTempDir
- interface FSImplementation
- interface FSWatcherEventMap
- interface GlobOptions
- interface GlobOptionsWithFileTypes
- interface GlobOptionsWithoutFileTypes
- interface MakeDirectoryOptions
- interface ObjectEncodingOptions
- interface OpenAsBlobOptions
- interface OpenDirOptions
- interface ReadOptions
- interface ReadOptionsWithBuffer
- interface ReadStreamEventMap
- interface ReadStreamOptions
- interface ReadVResult
- interface RmOptions
- interface StatFsOptions
- interface StatOptions
- interface StatsBase
- interface StatsFsBase
- interface StatWatcher
Class: fs.StatWatcher
- interface StreamOptions
- interface Utf8StreamEventMap
- interface Utf8StreamOptions
- interface WatchFileOptions
Watch for changes on
filename. - interface WatchOptions
- interface WatchOptionsWithBufferEncoding
- interface WatchOptionsWithStringEncoding
- interface WriteOptions
- interface WriteStreamEventMap
- interface WriteStreamOptions
- interface WriteVResult
Types 15
- type BigIntStatsListener
- type BufferEncodingOption
- type EncodingOption
- type Mode
- type NoParamCallback
- type OpenMode
- type PathLike
Valid types for path values in "fs".
- type PathOrFileDescriptor
- type ReadPosition
- type StatsListener
- type TimeLike
- type WatchEventType
- type WatchIgnorePredicate
- type WatchListener
- type WriteFileOptions
Namespaces 41
- namespace fs.access
- namespace fs.appendFile
- namespace fs.chmod
- namespace fs.chown
- namespace fs.close
- namespace fs.constants
- namespace fs.copyFile
- namespace fs.fchmod
- namespace fs.fchown
- namespace fs.fdatasync
- namespace fs.fstat
- namespace fs.fsync
- namespace fs.ftruncate
- namespace fs.futimes
- namespace fs.lchown
- namespace fs.link
- namespace fs.lstat
- namespace fs.lutimes
- namespace fs.mkdir
- namespace fs.mkdtemp
- namespace fs.open
- namespace fs.opendir
- namespace fs.read
- namespace fs.readdir
- namespace fs.readFile
- namespace fs.readlink
- namespace fs.readv
- namespace fs.realpath
- namespace fs.realpathSync
- namespace fs.rename
- namespace fs.rm
- namespace fs.rmdir
- namespace fs.stat
- namespace fs.statfs
- namespace fs.symlink
- namespace fs.truncate
- namespace fs.unlink
- namespace fs.utimes
- namespace fs.write
- namespace fs.writeFile
- namespace fs.writev