Core ModulesLesson 23Free preview
File System (fs)
This lesson is part of Node.js · All levels
Summary
The fs module provides an API for reading, writing, modifying, and deleting files and folders on disk. There are two styles available: asynchronous that does not block the event loop and synchronous that returns the immediate results. For server applications, the asynchronous or Promise-based (fs/promises) method is highly recommended. (Node.js)
Key points
- Use fs.readFile and fs.writeFile for non-blocking file operations
- The method of sync (readFileSync) blocks the event loop, only for one run of the script
- Fs/promises with await more neatly for modern asynchronous grooves
- Always handle errors like files not found (ENOENT)
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.