Lesson 11 of 50 – Path Module
22% Completed
22%

Node.js Path Module

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.

In simple words: The Path module helps us create and manage file paths correctly in Node.js.

1. Importing the Path Module

The Path module is built into Node.js. We do not need to install any additional package to use it.

Example: Import Path
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';

2. The path.join() Method

The path.join() method joins multiple path segments into a single normalized path.

Example: Joining Paths
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.

3. The path.basename() Method

The path.basename() method returns the last part of a path, usually the filename.

Example: Getting a 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

4. The path.dirname() Method

The path.dirname() method returns the directory portion of a path.

Example: Getting a Directory
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.

5. The path.extname() Method

The path.extname() method returns the extension of a filename, including the dot.

Example: Getting a File Extension
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.

6. The path.parse() Method

The path.parse() method breaks a path into an object containing its root, directory, base filename, extension, and name.

Example: Parsing a Path
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.

7. The path.resolve() Method

The path.resolve() method creates an absolute path by resolving path segments from right to left until an absolute path is formed.

Example: Creating an Absolute Path
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.

8. The path.isAbsolute() Method

The path.isAbsolute() method checks whether a path is absolute according to the path rules of the current platform. It returns true or false.

Example: Checking a Path
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.

9. The path.normalize() Method

The path.normalize() method normalizes a path by resolving redundant separators and dot segments where possible.

Example: Normalizing a Path
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.

10. The path.sep Property

The path.sep property returns the path segment separator used by the current operating system.

Example: Displaying the Separator
const path = require('node:path');

console.log(path.sep);

Output on Windows:

\

Output on Linux or macOS:

/

11. Common Path Module Methods

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.

12. Practical Assignment

Practice Task
Create a Node.js program to work with file paths.
  1. Import the Path module.
  2. Create a path for students/data/student.txt using path.join().
  3. Display the filename using path.basename().
  4. Display the directory using path.dirname().
  5. Display the file extension using path.extname().
  6. Check whether a path is absolute using 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:

  • Filename: student.txt
  • Extension: .txt
  • Absolute: false for this relative path on normal Windows and POSIX environments.

🧠 Quiz – Node.js Path Module

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?