JavaScript:
Reading a text file
How to:
Here’s how you go about reading a text file in current JavaScript:
Using Node.js with Promises (Async/Await):
const fs = require('fs').promises;
async function readFile(filePath) {
try {
const data = await fs.readFile(filePath, 'utf8');
console.log(data);
} catch (error) {
console.error('Got an error trying to read the file:', error);
}
}
readFile('example.txt');
Sample output (contents of example.txt
):
Hello, this is a text file!
Using fetch API in the browser:
async function fetchTextFile(fileUrl) {
try {
const response = await fetch(fileUrl);
const text = await response.text();
console.log(text);
} catch (error) {
console.error('Oops, something went wrong fetching the file:', error);
}
}
fetchTextFile('example.txt');
Deep Dive
Originally, reading files in JavaScript was mostly a server-side affair, dealt with by Node.js. As JS danced into browsers with HTML5, APIs like FileReader
and fetch
arrived, making client-side file reading possible without a sweat.
Alternatives? Oh, there are a few. Streams can handle big files without hogging memory. Workers prevent UI freeze-ups. Libraries make complex tasks easier. Each has its place.
Under the hood, file reading may involve buffer management, character encoding (UTF-8, etc.), and error handling. Be mindful of security, too; browsers restrict file access for good reasons.
See Also
Take your learning further with these resources:
- MDN’s FileReader API Doc: MDN FileReader
- Node.js File System Docs: Node.js fs
- Stream API for big files: Node.js stream
- Understanding fetch API: MDN fetch