0
0
Node.jsframework~20 mins

Creating and removing directories in Node.js - Practice Exercises

Choose your learning style9 modes available
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.