namespace
fs.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 buffer is provided and no encoding is specified, the returned Buffer is a view over the supplied buffer containing only the bytes read. If the supplied buffer is too small to contain the entire file, the callback is called with an error.
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 fs.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.
An example using the buffer option with a pre-allocated buffer:
import { Buffer } from 'node:buffer';
import { readFile } from 'node:fs';
const buf = Buffer.alloc(16384);
readFile('/path/to/file', { buffer: buf }, (err, data) => {
if (err) throw err;
console.log(data); // A view over `buf` containing only the bytes read
});An example using the buffer option with a function returning a buffer:
import { Buffer } from 'node:buffer';
import { readFile } from 'node:fs';
readFile('/path/to/file', {
buffer: (size) => Buffer.alloc(size),
}, (err, data) => {
if (err) throw err;
console.log(data);
});filename or file descriptor
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 buffer is provided and no encoding is specified, the returned Buffer is a view over the supplied buffer containing only the bytes read. If the supplied buffer is too small to contain the entire file, the callback is called with an error.
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 fs.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.
An example using the buffer option with a pre-allocated buffer:
import { Buffer } from 'node:buffer';
import { readFile } from 'node:fs';
const buf = Buffer.alloc(16384);
readFile('/path/to/file', { buffer: buf }, (err, data) => {
if (err) throw err;
console.log(data); // A view over `buf` containing only the bytes read
});An example using the buffer option with a function returning a buffer:
import { Buffer } from 'node:buffer';
import { readFile } from 'node:fs';
readFile('/path/to/file', {
buffer: (size) => Buffer.alloc(size),
}, (err, data) => {
if (err) throw err;
console.log(data);
});filename or file descriptor
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 buffer is provided and no encoding is specified, the returned Buffer is a view over the supplied buffer containing only the bytes read. If the supplied buffer is too small to contain the entire file, the callback is called with an error.
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 fs.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.
An example using the buffer option with a pre-allocated buffer:
import { Buffer } from 'node:buffer';
import { readFile } from 'node:fs';
const buf = Buffer.alloc(16384);
readFile('/path/to/file', { buffer: buf }, (err, data) => {
if (err) throw err;
console.log(data); // A view over `buf` containing only the bytes read
});An example using the buffer option with a function returning a buffer:
import { Buffer } from 'node:buffer';
import { readFile } from 'node:fs';
readFile('/path/to/file', {
buffer: (size) => Buffer.alloc(size),
}, (err, data) => {
if (err) throw err;
console.log(data);
});filename or file descriptor
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 buffer is provided and no encoding is specified, the returned Buffer is a view over the supplied buffer containing only the bytes read. If the supplied buffer is too small to contain the entire file, the callback is called with an error.
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 fs.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.
An example using the buffer option with a pre-allocated buffer:
import { Buffer } from 'node:buffer';
import { readFile } from 'node:fs';
const buf = Buffer.alloc(16384);
readFile('/path/to/file', { buffer: buf }, (err, data) => {
if (err) throw err;
console.log(data); // A view over `buf` containing only the bytes read
});An example using the buffer option with a function returning a buffer:
import { Buffer } from 'node:buffer';
import { readFile } from 'node:fs';
readFile('/path/to/file', {
buffer: (size) => Buffer.alloc(size),
}, (err, data) => {
if (err) throw err;
console.log(data);
});filename or file descriptor
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 buffer is provided and no encoding is specified, the returned Buffer is a view over the supplied buffer containing only the bytes read. If the supplied buffer is too small to contain the entire file, the callback is called with an error.
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 fs.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.
An example using the buffer option with a pre-allocated buffer:
import { Buffer } from 'node:buffer';
import { readFile } from 'node:fs';
const buf = Buffer.alloc(16384);
readFile('/path/to/file', { buffer: buf }, (err, data) => {
if (err) throw err;
console.log(data); // A view over `buf` containing only the bytes read
});An example using the buffer option with a function returning a buffer:
import { Buffer } from 'node:buffer';
import { readFile } from 'node:fs';
readFile('/path/to/file', {
buffer: (size) => Buffer.alloc(size),
}, (err, data) => {
if (err) throw err;
console.log(data);
});filename or file descriptor