Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why file system access matters
📖 Scenario: You are building a simple Node.js app that reads and writes text files on your computer. This helps you understand why file system access is important for many real-world apps like note-taking, data logging, or configuration management.
🎯 Goal: Create a Node.js script that reads a text file, counts the number of words, and writes the count to a new file.
📋 What You'll Learn
Use the built-in fs module to read and write files
Read the contents of a file named input.txt
Count the number of words in the file content
Write the word count to a file named output.txt
💡 Why This Matters
🌍 Real World
Many apps need to read and save data on your computer, like text editors, loggers, or configuration tools. Understanding file system access lets you build these apps.
💼 Career
Node.js developers often work with files for data processing, automation, and backend services. Knowing how to use the file system module is a key skill.
Progress0 / 4 steps
1
Set up the file system module and read the file
Write import fs from 'fs/promises' to import the file system promises API. Then create an async function called countWords that reads the file input.txt using await fs.readFile('input.txt', 'utf-8') and stores the content in a variable called text.
Node.js
Hint
Use import fs from 'fs/promises' to get the modern promise-based file system API. Use await fs.readFile inside an async function to read the file content as text.
2
Add a variable to count words
Inside the countWords function, create a variable called words that splits the text by spaces using text.split(' '). Then create a variable called wordCount that stores the length of the words array.
Node.js
Hint
Use text.split(' ') to break the text into words. Then use words.length to count how many words there are.
3
Write the word count to a new file
Still inside the countWords function, use await fs.writeFile('output.txt', `Word count: ${wordCount}`) to write the word count to a file named output.txt.
Node.js
Hint
Use await fs.writeFile to save the word count string to output.txt.
4
Call the function to run the script
After the countWords function, add a line to call countWords() so the script runs when executed.
Node.js
Hint
Simply call countWords() after the function to run the code.
Practice
(1/5)
1. Why is file system access important in a Node.js application?
easy
A. It prevents the app from using the internet.
B. It makes the app run faster by using more CPU cores.
C. It automatically fixes bugs in the code.
D. It allows the app to save and load data outside the program.
Solution
Step 1: Understand file system role
File system access lets an app save and retrieve data from files on the computer.
Step 2: Identify correct purpose
Only 'It allows the app to save and load data outside the program.' correctly describes this purpose; others are unrelated to file system access.
Final Answer:
It allows the app to save and load data outside the program. -> Option D
Quick Check:
File system access = save/load data [OK]
Hint: File system means saving or reading files outside code [OK]
Common Mistakes:
Confusing file system access with CPU or internet features
Thinking file system fixes bugs automatically
Assuming file system access speeds up CPU usage
2. Which of the following is the correct way to import the Node.js file system module?
easy
A. require fs from 'fs';
B. const fs = require('fs');
C. const fs = import('fs');
D. import fs from 'fs';
Solution
Step 1: Recall Node.js import syntax
Node.js commonly uses const fs = require('fs'); to import modules in CommonJS style.
Step 2: Check options for syntax correctness
'require fs from 'fs';' is invalid syntax; 'import fs from 'fs';' is ES module style but requires extra config; 'const fs = import('fs');' uses dynamic import returning a Promise; 'const fs = require('fs');' matches correct syntax.
Final Answer:
const fs = require('fs'); -> Option B
Quick Check:
CommonJS import = require('fs') [OK]
Hint: Use require('fs') to import file system in Node.js [OK]
A. Missing encoding, so data is a Buffer, not string.
B. Callback function is missing the error parameter.
C. readFile is a synchronous method, cannot use callback.
D. fs module is not imported correctly.
Solution
Step 1: Check readFile usage
fs.readFile without encoding returns a Buffer object, not a string.
Step 2: Understand console.log output
Logging Buffer prints raw bytes, not readable text. To get string, encoding like 'utf8' is needed.
Final Answer:
Missing encoding, so data is a Buffer, not string. -> Option A
Quick Check:
readFile without encoding = Buffer data [OK]
Hint: Add 'utf8' encoding to readFile for string data [OK]
Common Mistakes:
Ignoring that data is a Buffer without encoding
Thinking readFile is synchronous
Assuming callback lacks error parameter
5. You want to read multiple files asynchronously and process their contents only after all are read. Which approach best uses Node.js file system access to achieve this?
hard
A. Use fs.readFile with callbacks and count completed reads before processing.
B. Use fs.readFileSync for each file in a loop to block until done.
C. Use fs.writeFile to write all files first, then read one file.
D. Use fs.unlink to delete files before reading them.
Solution
Step 1: Understand asynchronous reading need
Reading files asynchronously avoids blocking the app and keeps it responsive.
Step 2: Choose method to track multiple async reads
Using fs.readFile with callbacks and counting completions lets you know when all files are read before processing.
Step 3: Eliminate incorrect options
Using fs.readFileSync for each file in a loop blocks until done and is inefficient; using fs.writeFile writes files unnecessarily; using fs.unlink deletes files, which is wrong.
Final Answer:
Use fs.readFile with callbacks and count completed reads before processing. -> Option A
Quick Check:
Async read with callbacks + count = best approach [OK]
Hint: Use async readFile with callback counting to wait all done [OK]