0
0
Node.jsframework~15 mins

Creating and removing directories in Node.js - Try It Yourself

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Use await fs.rmdir('test-folder'); to remove the directory and call the function with manageDirectory();.