Bird
Raised Fist0
Node.jsframework~20 mins

Creating and removing directories in Node.js - Practice Exercises

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Directory Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of directory creation with recursive option
What will be the output of this Node.js code snippet when creating nested directories?
Node.js
import { mkdirSync } from 'node:fs';

try {
  mkdirSync('parent/child/grandchild', { recursive: true });
  console.log('Directories created');
} catch (error) {
  console.log('Error:', error.message);
}
ANo output
BError: ENOENT: no such file or directory, mkdir 'parent/child/grandchild'
CDirectories created
DError: EEXIST: file already exists, mkdir 'parent/child/grandchild'
Attempts:
2 left
💡 Hint
The recursive option allows creating nested directories even if parent folders don't exist.
Predict Output
intermediate
2:00remaining
Behavior of rmdirSync on non-empty directory
What happens when this Node.js code tries to remove a non-empty directory?
Node.js
import { rmdirSync } from 'node:fs';

try {
  rmdirSync('myFolder');
  console.log('Directory removed');
} catch (error) {
  console.log(error.code);
}
ADirectory removed
BEACCES
CENOENT
DENOTEMPTY
Attempts:
2 left
💡 Hint
rmdirSync cannot remove directories that contain files or folders unless recursive option is used.
📝 Syntax
advanced
2:00remaining
Correct syntax for recursive directory removal
Which option correctly removes a directory and all its contents recursively in Node.js?
ArmdirSync('folder', { recursive: true });
BrmdirSync('folder', true);
CrmdirSync('folder', { force: true });
DrmdirSync('folder', { deep: true });
Attempts:
2 left
💡 Hint
The recursive option is an object property, not a boolean argument or other name.
component_behavior
advanced
2:00remaining
Effect of mkdirSync without recursive on nested path
What happens when mkdirSync is called without recursive on a nested path where parent folders don't exist?
Node.js
import { mkdirSync } from 'node:fs';

try {
  mkdirSync('a/b/c');
  console.log('Created');
} catch (error) {
  console.log(error.code);
}
AENOENT
BEEXIST
CCreated
DEPERM
Attempts:
2 left
💡 Hint
Without recursive:true, mkdirSync cannot create nested directories if parents are missing.
🔧 Debug
expert
2:00remaining
Why does this directory removal code fail?
Given this code snippet, why does it throw an error when trying to remove 'tempDir'?
Node.js
import { rmdirSync } from 'node:fs';

rmdirSync('tempDir');
AtempDir path is incorrect, causing ENOENT error
BtempDir contains files or subdirectories, so rmdirSync fails without recursive:true
CrmdirSync requires a callback function, so this call is invalid
DrmdirSync cannot remove directories on Windows
Attempts:
2 left
💡 Hint
Check if the directory is empty before removing without recursive option.

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

  1. Step 1: Understand directory creation methods

    Node.js provides fs.mkdir() to create directories.
  2. Step 2: Differentiate from removal methods

    fs.rmdir() is for removing directories, not creating.
  3. Final Answer:

    fs.mkdir() -> Option D
  4. 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

  1. Step 1: Recognize recursive option for nested folders

    To create nested directories, use { recursive: true } option with fs.mkdir().
  2. 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.
  3. Final Answer:

    fs.mkdir('parent/child', { recursive: true }, callback) -> Option B
  4. 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?
const fs = require('fs');
fs.rmdir('myFolder', (err) => {
  if (err) console.log('Error:', err.message);
  else console.log('Folder removed');
});

Assuming myFolder contains files.
medium
A. Folder is removed successfully
B. Code throws a syntax error
C. Error logged because folder is not empty
D. Folder is renamed instead of removed

Solution

  1. Step 1: Understand fs.rmdir behavior

    fs.rmdir only removes empty directories; if folder has files, it throws an error.
  2. Step 2: Analyze error handling in callback

    The callback logs error message if removal fails due to non-empty folder.
  3. Final Answer:

    Error logged because folder is not empty -> Option C
  4. Quick Check:

    Non-empty folder removal = error [OK]
Hint: fs.rmdir fails if folder has files; must delete files first [OK]
Common Mistakes:
  • Assuming fs.rmdir removes non-empty folders
  • Expecting folder rename instead of removal
  • Thinking code throws syntax error
4. Identify the error in this code snippet that tries to remove a directory:
const fs = require('fs');
fs.rmdir('testDir', { recursive: true }, (err) => {
  if (err) throw err;
  console.log('Directory removed');
});
medium
A. fs.rmdir does not support recursive option
B. Callback function is missing
C. fs.rmdir cannot remove directories
D. Directory path must be absolute

Solution

  1. Step 1: Check fs.rmdir method options

    fs.rmdir does not support the recursive option; fs.rm should be used instead for recursive removal.
  2. Step 2: Understand correct method for recursive removal

    Use fs.rm with { recursive: true } to remove non-empty directories recursively.
  3. Final Answer:

    fs.rmdir does not support recursive option -> Option A
  4. 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

  1. Step 1: Create nested directories

    Use fs.mkdir with { recursive: true } to create nested folders in one step.
  2. Step 2: Remove directory with all contents

    Use fs.rm with { recursive: true, force: true } to delete directory and all files inside safely.
  3. Final Answer:

    Use fs.mkdir with { recursive: true } to create, then fs.rm with { recursive: true, force: true } to remove -> Option A
  4. Quick Check:

    Nested create + recursive remove = fs.mkdir + fs.rm [OK]
Hint: Create nested with mkdir recursive, remove all with rm recursive force [OK]
Common Mistakes:
  • Using fs.rmdir to remove non-empty directories
  • Omitting recursive option when creating nested folders
  • Using fs.unlink which deletes files, not folders