Introduction
The Node.js path module provides useful methods for working with file and directory paths. It helps developers create paths, find filenames, identify file extensions, join folders, and work with absolute and relative paths. The path module is especially useful when building file-handling applications because different operating systems use different path separators. In this chapter, you will practice the most important path methods with simple, step-by-step examples for Node.js Path module practice questions.
Question 1: How do you import the Path module in Node.js?
Problem
Import the built-in path module and create a simple file path.
Solution
const path = require("path");
const filePath = path.join("documents", "notes.txt");
console.log(filePath);
Output
On Windows:
documents\notes.txt
On Linux or macOS:
documents/notes.txt
Step-by-Step Explanation
- Import the built-in
pathmodule usingrequire(). - Store the module in the
pathvariable. - Use
path.join()to combine folder and file names. - Node.js automatically uses the correct path separator for the operating system.
- Display the final path.
The path module is already included with Node.js, so you do not need to install it.
Question 2: How do you join multiple paths using path.join()?
Problem
Create a path for a student profile file inside multiple folders.
Solution
const path = require("path");
const filePath = path.join(
"website",
"users",
"students",
"profile.txt"
);
console.log(filePath);
Output
On Windows:
website\users\students\profile.txt
On Linux or macOS:
website/users/students/profile.txt
Step-by-Step Explanation
- Import the
pathmodule. - Call
path.join(). - Provide each folder as a separate argument.
- Add the filename as the final argument.
- Node.js joins all parts into one valid path.
- Display the result.
path.join() is one of the most commonly used methods in the Path module.
Question 3: How do you get the filename using path.basename()?
Problem
Extract the filename from a complete file path.
Solution
const path = require("path");
const filePath = "documents/students/profile.js";
const fileName = path.basename(filePath);
console.log(fileName);
Output
profile.js
Step-by-Step Explanation
- Import the
pathmodule. - Store the complete file path.
- Use
path.basename(). - The method returns the last part of the path.
- In this example, the last part is
profile.js.
This is useful when you have a complete path but only need the filename.
Question 4: How do you get the file extension using path.extname()?
Problem
Find the extension of a JavaScript file.
Solution
const path = require("path");
const filePath = "projects/node/app.js";
const extension = path.extname(filePath);
console.log(extension);
Output
.js
Step-by-Step Explanation
- Import the
pathmodule. - Store the file path.
- Use
path.extname(). - Node.js checks the filename.
- The method returns
.js.
For example:
path.extname("photo.jpg");
returns:
.jpg
Question 5: How do you get the directory name using path.dirname()?
Problem
Find the directory portion of a file path.
Solution
const path = require("path");
const filePath = "website/images/logo.png";
const directory = path.dirname(filePath);
console.log(directory);
Output
website/images
Step-by-Step Explanation
- Import the
pathmodule. - Store the file path.
- Use
path.dirname(). - The method removes the filename from the path.
- The remaining part is returned as the directory path.
So:
website/images/logo.png
becomes:
website/images
Question 6: How do you get the filename without its extension?
Problem
Extract the filename from a path and remove its extension.
Solution
const path = require("path");
const filePath = "documents/report.pdf";
const fileName = path.basename(filePath, path.extname(filePath));
console.log(fileName);
Output
report
Step-by-Step Explanation
- Import the
pathmodule. - Store the complete file path.
path.extname()finds.pdf.path.basename()gets the filename.- The extension is passed as the second argument.
- The result becomes
report.
This technique is useful when you need the filename without its extension.
Question 7: How do you check whether a path is absolute?
Problem
Check whether a given path is an absolute path.
Solution
const path = require("path");
const filePath = "/home/user/documents/file.txt";
console.log(path.isAbsolute(filePath));
Output
On systems where the given path is absolute:
true
Step-by-Step Explanation
- Import the
pathmodule. - Store the path in a variable.
- Use
path.isAbsolute(). - The method checks whether the path starts from the root location.
- It returns either
trueorfalse.
For example:
path.isAbsolute("/home/user/file.txt");
returns:
true
While:
path.isAbsolute("documents/file.txt");
returns:
false
Question 8: How do you normalize a path using path.normalize()?
Problem
Clean up a path containing unnecessary . and .. segments.
Solution
const path = require("path");
const filePath = "users/../documents/./notes.txt";
const cleanPath = path.normalize(filePath);
console.log(cleanPath);
Output
On Linux or macOS:
documents/notes.txt
On Windows, the separator may appear as \.
Step-by-Step Explanation
The path contains:
users/../
The .. means to move one directory backward.
It also contains:
./
which represents the current directory.
path.normalize() cleans these unnecessary parts and produces a normalized path.
Question 9: How do you create an absolute path using path.resolve()?
Problem
Create an absolute path for a file named data.txt.
Solution
const path = require("path");
const absolutePath = path.resolve(
"documents",
"students",
"data.txt"
);
console.log(absolutePath);
Output
The exact output depends on your computer.
For example:
C:\Projects\myapp\documents\students\data.txt
Step-by-Step Explanation
- Import the
pathmodule. - Use
path.resolve(). - Provide the directory and filename.
- Node.js starts from the current working directory when resolving the relative parts.
- It creates an absolute path.
- Display the result.
The important difference is:
path.join()combines path segments.path.resolve()produces an absolute path.
Question 10: How do you use multiple Path module methods together?
Problem
Given a file path, display:
- Complete path
- Directory name
- Filename
- File extension
- Filename without extension
Solution
const path = require("path");
const filePath = path.join(
"website",
"uploads",
"images",
"profile.jpg"
);
const directory = path.dirname(filePath);
const fileName = path.basename(filePath);
const extension = path.extname(filePath);
const nameWithoutExtension = path.basename(
filePath,
extension
);
console.log("Path:", filePath);
console.log("Directory:", directory);
console.log("Filename:", fileName);
console.log("Extension:", extension);
console.log("Name:", nameWithoutExtension);
Output
On Linux or macOS:
Path: website/uploads/images/profile.jpg
Directory: website/uploads/images
Filename: profile.jpg
Extension: .jpg
Name: profile
On Windows, the path separators may appear as \.
Step-by-Step Explanation
- Import the
pathmodule. - Use
path.join()to create the complete path. - Use
path.dirname()to get the directory. - Use
path.basename()to get the filename. - Use
path.extname()to get the extension. - Use
path.basename()again to remove the extension. - Display all the information.
This combines several important Path module methods into one practical example.
Key Takeaways
- The
pathmodule is a built-in Node.js module. - You can import it using
require("path"). path.join()combines multiple path segments.path.basename()returns the filename from a path.path.dirname()returns the directory portion of a path.path.extname()returns the file extension.path.isAbsolute()checks whether a path is absolute.path.normalize()cleans and normalizes a path.path.resolve()creates an absolute path.- The Path module helps make file and directory operations more reliable across operating systems.
- Using Path methods is safer than manually constructing paths with hard-coded separators.
FAQs
1. What is the Path module in Node.js?
The Path module is a built-in Node.js module used for working with file and directory paths. It provides methods for joining, resolving, normalizing, and analyzing paths.
2. Do I need to install the Node.js Path module?
No. The path module is included with Node.js.
You can use it directly:
const path = require("path");
3. What does path.join() do?
path.join() combines multiple path segments into a single path.
const path = require("path");
console.log(path.join("users", "documents", "file.txt"));
4. What does path.basename() do?
path.basename() returns the final part of a path, usually the filename.
path.basename("documents/report.pdf");
Output:
report.pdf
5. What does path.extname() do?
path.extname() returns the extension of a file.
path.extname("image.jpg");
Output:
.jpg
6. What is the difference between path.join() and path.resolve()?
path.join() combines path segments.
path.join("users", "data", "file.txt");
path.resolve() resolves the segments into an absolute path.
path.resolve("users", "data", "file.txt");
7. Why should I use the Path module instead of manually creating paths?
Different operating systems use different path separators. The Path module handles these differences for you, making your Node.js applications more portable and reliable.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
