filename or file descriptor
Asynchronously reads the entire contents of a file.
A path to a file. If a URL is provided, it must use the file:
protocol.
If a file descriptor is provided, the underlying file will not be closed automatically.
Either the encoding for the result, or an object that contains the encoding and an optional flag.
If a flag is not provided, it defaults to 'r'
.
Asynchronously reads the entire contents of a file.
A path to a file. If a URL is provided, it must use the file:
protocol.
If a file descriptor is provided, the underlying file will not be closed automatically.
Either the encoding for the result, or an object that contains the encoding and an optional flag.
If a flag is not provided, it defaults to 'r'
.
Asynchronously reads the entire contents of a file.
A path to a file. If a URL is provided, it must use the file:
protocol.
If a file descriptor is provided, the underlying file will not be closed automatically.
Asynchronously reads the entire contents of a file.
The callback is passed two arguments
(err, data)
, wheredata
is the contents of the file.If no encoding is specified, then the raw buffer is returned.
If
options
is a string, then it specifies the encoding:When the path is a directory, the behavior of
fs.readFile()
and readFileSync is platform-specific. On macOS, Linux, and Windows, an error will be returned. On FreeBSD, a representation of the directory's contents will be returned.It is possible to abort an ongoing request using an
AbortSignal
. If a request is aborted the callback is called with anAbortError
:The
fs.readFile()
function buffers the entire file. To minimize memory costs, when possible prefer streaming viafs.createReadStream()
.Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering
fs.readFile
performs.Since
v0.1.29