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
Recall & Review
beginner
What Node.js module do you use to create and remove directories?
You use the fs module, which provides functions like mkdir to create and rmdir or rm to remove directories.
Click to reveal answer
intermediate
How do you create a directory including any missing parent folders in Node.js?
Use fs.mkdir(path, { recursive: true }). The recursive: true option creates all missing parent directories automatically.
Click to reveal answer
intermediate
What is the difference between fs.rmdir and fs.rm when removing directories?
fs.rmdir removes empty directories only. fs.rm with { recursive: true } can remove directories with files inside.
Click to reveal answer
beginner
What happens if you try to create a directory that already exists without the recursive option?
Node.js will throw an error because the directory already exists. Using { recursive: true } prevents this error by ignoring existing directories.
Click to reveal answer
advanced
How can you remove a directory and all its contents safely in Node.js?
Use fs.rm(path, { recursive: true, force: true }). This removes the directory and everything inside it without throwing errors if the directory doesn't exist.
Click to reveal answer
Which option makes fs.mkdir create all missing parent directories?
A{ recursive: false }
B{ recursive: true }
C{ force: true }
D{ createParents: true }
✗ Incorrect
The option { recursive: true } tells Node.js to create all missing parent directories.
Which method removes a directory only if it is empty?
Afs.mkdir
Bfs.rm with recursive
Cfs.rmdir
Dfs.unlink
✗ Incorrect
fs.rmdir removes empty directories only.
What does fs.rm(path, { force: true }) do?
ARemoves a directory and ignores errors if it doesn't exist
BCreates a directory forcibly
CRemoves files only
DThrows error if directory doesn't exist
✗ Incorrect
The force: true option ignores errors if the directory or file does not exist.
If you try to create a directory that exists without recursive option, what happens?
ADirectory is overwritten
BParent directories are created
CDirectory is ignored silently
DError is thrown
✗ Incorrect
Node.js throws an error if the directory exists and recursive option is not used.
Which Node.js module provides directory creation and removal functions?
Afs
Bpath
Chttp
Dos
✗ Incorrect
The fs module handles file system operations including directories.
Explain how to create a directory and all its missing parent directories in Node.js.
Think about the option that allows creating multiple levels of folders at once.
You got /4 concepts.
Describe the difference between removing an empty directory and removing a directory with files inside in Node.js.
Consider which methods allow deleting directories with content and which do not.
You got /4 concepts.
Practice
(1/5)
1. Which Node.js method is used to create a new directory?
easy
A. fs.createDir()
B. fs.rmdir()
C. fs.deleteDir()
D. fs.mkdir()
Solution
Step 1: Understand directory creation methods
Node.js provides fs.mkdir() to create directories.
Step 2: Differentiate from removal methods
fs.rmdir() is for removing directories, not creating.
Final Answer:
fs.mkdir() -> Option D
Quick Check:
Create directory = fs.mkdir() [OK]
Hint: Use fs.mkdir() to create folders, not fs.rmdir() [OK]
Common Mistakes:
Confusing fs.mkdir() with fs.rmdir()
Using non-existent fs.createDir()
Trying to use fs.deleteDir() which doesn't exist
2. Which of the following is the correct syntax to create a nested directory parent/child in Node.js?
easy
A. fs.mkdir('parent/child', callback)
B. fs.mkdir('parent/child', { recursive: true }, callback)
C. fs.rmdir('parent/child', { recursive: true }, callback)
D. fs.mkdir('parent/child', { nested: true }, callback)
Solution
Step 1: Recognize recursive option for nested folders
To create nested directories, use { recursive: true } option with fs.mkdir().
Step 2: Confirm correct method and options
fs.mkdir with recursive true creates all needed folders; fs.rmdir is for removal, and nested option doesn't exist.
Final Answer:
fs.mkdir('parent/child', { recursive: true }, callback) -> Option B
Quick Check:
Nested folders need recursive: true [OK]
Hint: Use { recursive: true } with fs.mkdir for nested folders [OK]
Common Mistakes:
Omitting recursive option for nested directories
Using fs.rmdir instead of fs.mkdir
Using incorrect option name like nested
3. What will happen if you run the following code snippet in Node.js?
fs.rmdir does not support the recursive option; fs.rm should be used instead for recursive removal.
Step 2: Understand correct method for recursive removal
Use fs.rm with { recursive: true } to remove non-empty directories recursively.
Final Answer:
fs.rmdir does not support recursive option -> Option A
Quick Check:
Recursive removal needs fs.rm, not fs.rmdir [OK]
Hint: Use fs.rm for recursive removal, not fs.rmdir [OK]
Common Mistakes:
Using recursive option with fs.rmdir
Thinking callback is missing
Believing directory path must be absolute
5. You want to create a nested directory projects/app/src and then remove it completely including all files inside. Which sequence of Node.js methods should you use?
hard
A. Use fs.mkdir with { recursive: true } to create, then fs.rm with { recursive: true, force: true } to remove
B. Use fs.mkdir without options to create, then fs.rmdir to remove
C. Use fs.mkdir with { recursive: true } to create, then fs.rmdir to remove
D. Use fs.mkdir to create, then fs.unlink to remove
Solution
Step 1: Create nested directories
Use fs.mkdir with { recursive: true } to create nested folders in one step.
Step 2: Remove directory with all contents
Use fs.rm with { recursive: true, force: true } to delete directory and all files inside safely.
Final Answer:
Use fs.mkdir with { recursive: true } to create, then fs.rm with { recursive: true, force: true } to remove -> Option A