Path Module (Node.
js)
The Path Module in [Link] is a built-in module used to work with file and directory paths. It helps you create, modify,
and extract information from file paths without worrying about different operating systems (Windows, Linux, macOS).
To use the Path module:
const path = require("path");
1. What is Path Module?
The Path Module provides utilities for working with file and folder paths.
Why use Path Module?
• Join folder names safely
• Get filename from a path
• Get file extension
• Parse a path into different parts
• Make your code work on Windows and Linux
Example
const path = require("path");
[Link](path);
Output (simplified)
Object {
resolve,
join,
basename,
dirname,
extname,
parse,
...
}
2. basename()
The basename() method returns only the file name from a complete path.
Syntax
[Link](path)
Example
const path = require("path");
const filePath = " D:/Node Js/ 1. Module 1 [Link]";
[Link]([Link](filePath));
Output
[Link]
Removing the Extension
const path = require("path");
const filePath = “D:/Node Js/ 1. Module 1 [Link]";
[Link]([Link](filePath, ".pdf"));
Output
report
Example
Suppose a user uploads
C:/Uploads/[Link]
You only want the filename.
const path = require("path");
const upload = "C:/Uploads/[Link]";
const fileName = [Link](upload);
[Link](fileName);
Output
[Link]
3. extname()
The extname() method returns the extension of a file.
Syntax
[Link](path)
Example
const path = require("path");
const filePath = "D:/Node Js/1. Module 1 [Link]";
[Link]([Link](filePath));
Output
.pdf
Examples
const path = require("path");
[Link]([Link]("[Link]"));
[Link]([Link]("music.mp3"));
[Link]([Link]("movie.mp4"));
Output
.png
.mp3
.mp4
Example
Only allow PDF files.
const path = require("path");
const file = "[Link]";
if ([Link](file) === ".pdf") {
[Link]("Valid PDF");
} else {
[Link]("Invalid File");
}
Output
Valid PDF
4. join()
The join() method combines multiple path segments into a single valid path.
It automatically adds the correct separator (/ or \) based on the operating system.
Syntax
[Link](part1, part2, part3)
Example
const path = require("path");
const fullPath = [Link]("Users", "Aryan", "Desktop");
[Link](fullPath);
Windows Output
Users\Aryan\Desktop
Another Example
const path = require("path");
const file = [Link]("Documents", "Projects", "node", "[Link]");
[Link](file);
Output (Windows)
Documents\Projects\node\[Link]
Example
Creating an uploads folder path.
const path = require("path");
const uploadFolder = [Link](__dirname, "uploads");
[Link](uploadFolder);
Output
C:\NodeProject\uploads
5. parse()
The parse() method converts a path into an object containing its different parts.
Syntax
[Link](path)
Example
const path = require("path");
const filePath = "C:/Users/Aryan/Documents/[Link]";
[Link]([Link](filePath));
Output
{
root: 'C:/',
dir: 'C:/Users/Aryan/Documents',
base: '[Link]',
ext: '.pdf',
name: 'report'
}
Understanding Each Property
Property Description
root Drive or root directory
dir Folder path
base File name with extension
ext File extension
name File name without extension
Access Individual Properties
const path = require("path");
const filePath = "C:/Users/Aryan/Documents/[Link]";
const details = [Link](filePath);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
Output
report
.pdf
[Link]
Complete Example
const path = require("path");
const file = "C:/Users/Aryan/Desktop/[Link]";
[Link]("Basename:", [Link](file));
[Link]("Extension:", [Link](file));
[Link]("Joined Path:", [Link]("Users", "Aryan", "Desktop"));
const info = [Link](file);
[Link]("Name:", [Link]);
[Link]("Directory:", [Link]);
[Link]("Extension:", [Link]);
Output
Basename: [Link]
Extension: .pdf
Joined Path: Users\Aryan\Desktop
Name: Resume
Directory: C:/Users/Aryan/Desktop
Extension: .pdf
Summary Table
Method Purpose Example Output
[Link]() Returns the file name [Link]("docs/[Link]") [Link]
[Link]() Returns the file [Link]("[Link]") .txt
extension
[Link]() Joins path segments [Link]("docs","[Link]") docs/[Link] (or docs\[Link] on
safely Windows)
[Link]() Breaks a path into an [Link]("docs/[Link]") { dir, base, ext, name }
object
Exercises
1. Extract the filename from D:/Projects/Node/[Link].
2. Extract the extension from [Link].
3. Join these folders into a valid path: "Users", "Admin", "Downloads", "movie.mp4".
4. Parse the path C:/Users/Ali/Documents/[Link] and print only:
o Directory
o File name
o Extension
5. Create a path for an images folder inside the current project using __dirname and [Link]().
6. Check whether the uploaded file [Link] has the .pdf extension using [Link]().