0
0
Node.jsframework~10 mins

path.basename and path.dirname in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - path.basename and path.dirname
Input: full file path
Extract file name
Extract directory path
Output: base name and directory name
The flow shows how a full file path is split into the file name using basename and the directory path using dirname.
Execution Sample
Node.js
const path = require('path');
const fullPath = '/home/user/docs/file.txt';
const base = path.basename(fullPath);
const dir = path.dirname(fullPath);
console.log(base, dir);
This code extracts the file name and directory path from a full file path string.
Execution Table
StepActionInputOutput
1Call path.basename(fullPath)/home/user/docs/file.txtfile.txt
2Call path.dirname(fullPath)/home/user/docs/file.txt/home/user/docs
3Print base and dirbase='file.txt', dir='/home/user/docs'Output: 'file.txt /home/user/docs'
4EndNo more actionsExecution complete
💡 All functions executed, outputs derived from input path string
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
fullPath/home/user/docs/file.txt/home/user/docs/file.txt/home/user/docs/file.txt/home/user/docs/file.txt/home/user/docs/file.txt
baseundefinedfile.txtfile.txtfile.txtfile.txt
dirundefinedundefined/home/user/docs/home/user/docs/home/user/docs
Key Moments - 2 Insights
Why does path.basename return only 'file.txt' and not the full path?
Because path.basename extracts only the last part of the path, which is the file name, as shown in execution_table step 1.
Why does path.dirname return the path without the file name?
Because path.dirname extracts everything before the last separator, which is the directory path, as shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of path.basename at step 1?
A/home/user/docs/file.txt
Bfile.txt
C/home/user/docs
Ddocs
💡 Hint
Check the Output column in execution_table row for step 1
At which step does the variable 'dir' get its value?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at variable_tracker row for 'dir' and see when it changes from undefined
If the input path was '/folder/sub/file.js', what would path.dirname return?
A/folder
Bfile.js
C/folder/sub
D/folder/sub/file.js
💡 Hint
Recall path.dirname returns the directory part before the last slash
Concept Snapshot
path.basename(path) returns the file name from a full path.
path.dirname(path) returns the directory path without the file name.
Both work by splitting the path string at the last separator.
Use these to separate file names and folders easily.
Full Transcript
This lesson shows how Node.js path module functions basename and dirname work. Given a full file path string, basename extracts the file name part, and dirname extracts the directory path part. The execution table traces calling basename and dirname on '/home/user/docs/file.txt', showing the outputs 'file.txt' and '/home/user/docs' respectively. Variables track these values step-by-step. Key moments clarify why basename returns only the file name and dirname returns the folder path. The quiz tests understanding of these outputs and variable changes. The snapshot summarizes usage: basename gets the file name, dirname gets the folder path from a full path string.