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
Creating and removing directories
📖 Scenario: You are building a simple Node.js script to manage folders for a project. You need to create a new directory and then remove it after some operations.
🎯 Goal: Build a Node.js script that creates a directory named test-folder and then removes it.
📋 What You'll Learn
Use the Node.js fs module
Create a directory named test-folder
Remove the directory named test-folder
Use asynchronous methods with await and async functions
💡 Why This Matters
🌍 Real World
Managing directories is common when working with file uploads, temporary files, or organizing project files automatically.
💼 Career
Node.js developers often need to create and clean up directories for applications, scripts, and deployment tasks.
Progress0 / 4 steps
1
Import the fs/promises module
Write a line to import the fs/promises module and assign it to a constant called fs.
Node.js
Hint
Use const fs = require('fs/promises'); to import the promises API of the fs module.
2
Create an async function called manageDirectory
Write an async function named manageDirectory with no parameters.
Node.js
Hint
Use async function manageDirectory() { } to define the function.
3
Inside manageDirectory, create a directory named test-folder
Inside the manageDirectory function, write a line that uses await fs.mkdir to create a directory named test-folder.
Node.js
Hint
Use await fs.mkdir('test-folder'); to create the directory.
4
Remove the directory test-folder inside manageDirectory and call the function
Inside the manageDirectory function, after creating the directory, write a line that uses await fs.rmdir to remove the directory named test-folder. Then, outside the function, call manageDirectory().
Node.js
Hint
Use await fs.rmdir('test-folder'); to remove the directory and call the function with manageDirectory();.
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