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
Using path.basename and path.dirname in Node.js
📖 Scenario: You are working on a Node.js script that processes file paths. You want to extract the file name and the directory name from a given file path.
🎯 Goal: Build a simple Node.js script that uses path.basename to get the file name and path.dirname to get the directory name from a file path string.
📋 What You'll Learn
Import the built-in path module
Create a variable with a specific file path string
Use path.basename to get the file name
Use path.dirname to get the directory name
💡 Why This Matters
🌍 Real World
File path manipulation is common in scripts that organize, move, or analyze files on your computer or server.
💼 Career
Understanding how to extract parts of file paths is useful for backend developers, DevOps engineers, and anyone working with file systems in Node.js.
Progress0 / 4 steps
1
Import the path module and create a file path variable
Write code to import the built-in path module using require('path'). Then create a variable called filePath and set it to the string "/home/user/docs/letter.txt".
Node.js
Hint
Use const path = require('path') to import the module. Then assign the exact string "/home/user/docs/letter.txt" to filePath.
2
Create variables for base name and directory name
Create two variables: baseName and dirName. Set baseName to the result of path.basename(filePath). Set dirName to the result of path.dirname(filePath).
Node.js
Hint
Use path.basename(filePath) to get the file name and path.dirname(filePath) to get the directory path.
3
Add a variable for file extension
Create a variable called extName and set it to the result of path.extname(filePath) to get the file extension.
Node.js
Hint
Use path.extname(filePath) to get the file extension like '.txt'.
4
Export the variables as a module
Add a module.exports statement to export an object with baseName, dirName, and extName properties.
Node.js
Hint
Use module.exports = { baseName, dirName, extName } to export the variables.
Practice
(1/5)
1. What does path.basename return when given a full file path?
easy
A. The folder path only
B. The file name with extension
C. The full path unchanged
D. The file extension only
Solution
Step 1: Understand path.basename purpose
This function extracts the last part of a path, which is usually the file name with its extension.
Step 2: Compare with other path parts
Unlike path.dirname which returns the folder path, path.basename returns the file name part.
A. Incorrect method name 'baseName' should be 'basename'
B. Missing import of 'path' module
C. Wrong argument type passed to dirname
D. No error, code runs fine
Solution
Step 1: Check method names used
The method path.baseName is incorrect because the correct method is all lowercase basename.
Step 2: Verify other parts
The path module is imported correctly and dirname usage is correct, so no other errors.
Final Answer:
Incorrect method name 'baseName' should be 'basename' -> Option A
Quick Check:
Method names are case-sensitive [OK]
Hint: Check method name spelling and case carefully [OK]
Common Mistakes:
Using wrong case in method names
Assuming method names are case-insensitive
Ignoring error messages about undefined functions
5. Given the path /home/user/docs/letter.txt, how can you use path.basename and path.dirname together to print: Folder: /home/user/docs File: letter.txt Which code snippet achieves this?
hard
A. console.log(`Folder: ${path.dirname(path.basename(filePath))}`);
console.log(`File: ${path.basename(path.dirname(filePath))}`);
B. console.log(`Folder: ${path.basename(filePath)}`);
console.log(`File: ${path.dirname(filePath)}`);
C. console.log(`Folder: ${path.dirname(filePath)}`);
console.log(`File: ${path.basename(filePath)}`);
D. console.log(`Folder: ${filePath.dirname()}`);
console.log(`File: ${filePath.basename()}`);
Solution
Step 1: Use path.dirname to get folder path
Calling path.dirname(filePath) returns the folder path /home/user/docs.
Step 2: Use path.basename to get file name
Calling path.basename(filePath) returns the file name letter.txt.
Step 3: Combine in template strings for output
Using template literals with these calls prints the desired output lines.
Final Answer:
console.log(`Folder: ${path.dirname(filePath)}`);
console.log(`File: ${path.basename(filePath)}`); -> Option C
Quick Check:
dirname = folder, basename = file [OK]
Hint: dirname for folder, basename for file name [OK]
Common Mistakes:
Swapping basename and dirname calls
Trying to call basename/dirname on strings directly