Synchronous version of readFile.
Reads the content of a file as a File object (blob encoding).
The absolute path of the file to read.
Read options with 'blob' encoding.
An IOResult containing a File object.
Synchronous version of readFile.
Reads the content of a file as a string (utf8 encoding).
The absolute path of the file to read.
Read options with 'utf8' encoding.
An IOResult containing the file content as a string.
Synchronous version of readFile.
Reads the content of a file as a Uint8Array (default).
The absolute path of the file to read.
Optionaloptions: ReadSyncOptions & { encoding?: "bytes" }Optional read options. Defaults to 'bytes' encoding.
An IOResult containing the file content as a Uint8Array.
Synchronous version of readFile.
Reads the content of a file with the specified options.
This overload accepts any ReadOptions and returns the union of all possible content types.
Useful when the encoding is determined at runtime.
The absolute path of the file to read.
Optionaloptions: ReadSyncOptionsOptional read options.
An IOResult containing the file content.
readFile for the async version.
// When encoding is dynamic
const encoding = getUserPreference(); // 'utf8' | 'bytes' | ...
readFileSync('/path/to/file.txt', { encoding })
.inspect(content => {
// content type is ReadSyncFileContent (union type)
if (typeof content === 'string') {
console.log('Text:', content);
} else if (content instanceof Uint8Array) {
console.log('Bytes:', content.length);
}
});
Synchronous version of
readFile. Reads the content of a file with the specified encoding.Param: filePath
The absolute path of the file to read.
Param: options
Optional read options.
Returns
An
IOResultcontaining the file content.See
readFile for the async version.