readFile
Asynchronously reads the entire contents of a file.
import { readFile } from 'node:fs';
readFile('/etc/passwd', (err, data) => {
if (err) throw err;
console.log(data);
});
The callback is passed two arguments (err, data)
, where data
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:
import { readFile } from 'node:fs';
readFile('/etc/passwd', 'utf8', callback);
When the path is a directory, the behavior of fs.readFile()
and {@link 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.
import { readFile } from 'node:fs';
// macOS, Linux, and Windows
readFile('<directory>', (err, data) => {
// => [Error: EISDIR: illegal operation on a directory, read <directory>]
});
// FreeBSD
readFile('<directory>', (err, data) => {
// => null, <data>
});
It is possible to abort an ongoing request using an AbortSignal
. If a request is aborted the callback is called with an AbortError
:
import { readFile } from 'node:fs';
const controller = new AbortController();
const signal = controller.signal;
readFile(fileInfo[0].name, { signal }, (err, buf) => {
// ...
});
// When you want to abort the request
controller.abort();
The fs.readFile()
function buffers the entire file. To minimize memory costs, when possible prefer streaming via fs.createReadStream()
.
Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering fs.readFile
performs.
Since
v0.1.29
Parameters
filename or file descriptor
Asynchronously reads the entire contents of a file.
Parameters
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.
Parameters
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.
If no encoding is specified (using options.encoding
), the data is returned as a Buffer
object. Otherwise, the data will be a string.
If options
is a string, then it specifies the encoding.
When the path
is a directory, the behavior of fsPromises.readFile()
is platform-specific. On macOS, Linux, and Windows, the promise will be rejected with an error. On FreeBSD, a representation of the directory's contents will be returned.
An example of reading a package.json
file located in the same directory of the running code:
import { readFile } from 'node:fs/promises';
try {
const filePath = new URL('./package.json', import.meta.url);
const contents = await readFile(filePath, { encoding: 'utf8' });
console.log(contents);
} catch (err) {
console.error(err.message);
}
It is possible to abort an ongoing readFile
using an AbortSignal
. If a request is aborted the promise returned is rejected with an AbortError
:
import { readFile } from 'node:fs/promises';
try {
const controller = new AbortController();
const { signal } = controller;
const promise = readFile(fileName, { signal });
// Abort the request before the promise settles.
controller.abort();
await promise;
} catch (err) {
// When a request is aborted - err is an AbortError
console.error(err);
}
Aborting an ongoing request does not abort individual operating system requests but rather the internal buffering fs.readFile
performs.
Any specified FileHandle
has to support reading.
Since
v10.0.0
Return
Fulfills with the contents of the file.
Parameters
filename or FileHandle
Asynchronously reads the entire contents of a file.
Parameters
A path to a file. If a URL is provided, it must use the file:
protocol. If a FileHandle
is provided, the underlying file will not be closed automatically.
An object that may contain an optional flag. If a flag is not provided, it defaults to 'r'
.