The Node.js Path module provides methods for working with file and directory paths. It helps us join paths, extract filenames, find file extensions, and resolve absolute paths.
The Path module is built into Node.js. We do not need to install any additional package to use it.
const path = require('node:path');
console.log(path);
This example uses CommonJS syntax to load the built-in Path module. If your project uses ES Modules, you can import it using:
import path from 'node:path';
The path.join() method joins multiple path segments
into a single normalized path.
const path = require('node:path');
const filePath = path.join('students', 'data', 'student.txt');
console.log(filePath);
Possible output on Windows:
students\data\student.txt
Possible output on Linux:
students/data/student.txt
The path separator depends on the operating system.
Using path.join() helps avoid manually
constructing platform-specific paths.
The path.basename() method returns the last part
of a path, usually the filename.
const path = require('node:path');
const filePath = '/home/student/notes.txt';
console.log(path.basename(filePath));
Output:
notes.txt
You can also remove a known suffix:
console.log(path.basename(filePath, '.txt'));
Output:
notes
The path.dirname() method returns the directory
portion of a path.
const path = require('node:path');
const filePath = '/home/student/notes.txt';
console.log(path.dirname(filePath));
Output on POSIX systems:
/home/student
The returned path format depends on the platform-specific path rules used by Node.js.
The path.extname() method returns the extension
of a filename, including the dot.
const path = require('node:path');
console.log(path.extname('index.html'));
console.log(path.extname('app.js'));
console.log(path.extname('photo.jpg'));
Output:
.html
.js
.jpg
If a filename has no extension, the method generally returns an empty string.
The path.parse() method breaks a path into an
object containing its root, directory, base filename,
extension, and name.
const path = require('node:path');
const result = path.parse('/home/student/notes.txt');
console.log(result.root);
console.log(result.dir);
console.log(result.base);
console.log(result.ext);
console.log(result.name);
Output on POSIX systems:
/
/home/student
notes.txt
.txt
notes
The second output is the directory path. Output details differ when using Windows paths.
The path.resolve() method creates an absolute path
by resolving path segments from right to left until an absolute
path is formed.
const path = require('node:path');
const result = path.resolve('students', 'data.txt');
console.log(result);
Possible output:
/your/current/folder/students/data.txt
The actual absolute path depends on your current working directory and operating system.
The path.isAbsolute() method checks whether a path
is absolute according to the path rules of the current platform.
It returns true or false.
const path = require('node:path');
console.log(path.isAbsolute('/home/student/file.txt'));
console.log(path.isAbsolute('students/file.txt'));
Output on POSIX systems:
true
false
An absolute path starts from the root of the filesystem. A relative path is interpreted in relation to another directory, such as the current working directory.
The path.normalize() method normalizes a path by
resolving redundant separators and dot segments where possible.
const path = require('node:path');
const result = path.normalize('students//data/../notes.txt');
console.log(result);
Possible output on POSIX systems:
students/notes.txt
Normalization makes the path more consistent, but it does not check whether the file actually exists.
The path.sep property returns the path segment
separator used by the current operating system.
const path = require('node:path');
console.log(path.sep);
Output on Windows:
\
Output on Linux or macOS:
/
| Method or Property | Purpose |
|---|---|
path.join() |
Joins path segments. |
path.basename() |
Returns the last part of a path. |
path.dirname() |
Returns the directory portion. |
path.extname() |
Returns the file extension. |
path.parse() |
Breaks a path into parts. |
path.resolve() |
Creates an absolute path. |
path.isAbsolute() |
Checks whether a path is absolute. |
path.normalize() |
Normalizes a path. |
path.sep |
Provides the platform-specific path separator. |
students/data/student.txt using path.join().path.basename().path.dirname().path.extname().path.isAbsolute().Starter code:
const path = require('node:path');
const filePath = path.join(
'students',
'data',
'student.txt'
);
console.log('Path:', filePath);
console.log('Filename:', path.basename(filePath));
console.log('Directory:', path.dirname(filePath));
console.log('Extension:', path.extname(filePath));
console.log('Absolute:', path.isAbsolute(filePath));
Expected results:
student.txt.txtfalse for this relative path on normal Windows and POSIX environments.Select the correct answer for each question.
1. Which method joins multiple path segments?
2. Which method returns a file's extension?
3. Which method creates an absolute path?