Challenge - 5 Problems
Directory Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); }
Attempts:
2 left
💡 Hint
The recursive option allows creating nested directories even if parent folders don't exist.
✗ Incorrect
Using mkdirSync with { recursive: true } creates all missing parent directories. So the nested path is created successfully and the message 'Directories created' is logged.
❓ Predict Output
intermediate2: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); }
Attempts:
2 left
💡 Hint
rmdirSync cannot remove directories that contain files or folders unless recursive option is used.
✗ Incorrect
rmdirSync throws an error with code 'ENOTEMPTY' if the directory is not empty and recursive option is not set.
📝 Syntax
advanced2:00remaining
Correct syntax for recursive directory removal
Which option correctly removes a directory and all its contents recursively in Node.js?
Attempts:
2 left
💡 Hint
The recursive option is an object property, not a boolean argument or other name.
✗ Incorrect
The correct syntax uses an options object with recursive:true to remove directories with contents.
❓ component_behavior
advanced2: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); }
Attempts:
2 left
💡 Hint
Without recursive:true, mkdirSync cannot create nested directories if parents are missing.
✗ Incorrect
The error code ENOENT means a parent directory does not exist, so mkdirSync fails without recursive.
🔧 Debug
expert2: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');
Attempts:
2 left
💡 Hint
Check if the directory is empty before removing without recursive option.
✗ Incorrect
rmdirSync throws an error if the directory is not empty and recursive:true is not specified.