Node.js Path Module (original) (raw)

Last Updated : 25 Apr, 2026

The path module provides utilities to handle and manipulate file and directory paths in a platform-independent way.

Installation Step (Optional)

Installation is an optional step as the path module is a built-in module in Node.js. Install the assert module using the following command:

npm install path

Importing the Module

To start using the path module in your Node.js application, import it as follows:

const path = require('path');

Path Methods

The path module provides methods to create, analyze, and manipulate file and directory paths in a platform-independent manner.

Method Description
path.join() Joins multiple path segments into a single path.
path.resolve() Resolves a sequence of paths into an absolute path.
path.normalize() Normalizes a path by resolving . and .. segments.
path.isAbsolute() Returns true if the path is an absolute.
path.basename() Returns the last portion of a path (file name).
path.dirname() Returns the directory name of a path.
path.extname() Returns the file extension of a path.
path.parse() Parses a path into an object (root, dir, base, etc.).
path.format() Constructs a path string from a parsed path object.
path.relative() Computes the relative path between two paths.
path.sep Platform-specific path separator (/ on POSIX, \ on Windows).
path.delimiter Platform-specific delimiter (: on POSIX, ; on Windows).

Features of Path Module

The Path module simplifies file path handling by providing consistent, cross-platform utilities.

**Example: Uses path.extname() to extract a file extension, useful for identifying or filtering file types.

JavaScript ``

console.clear(); const path = require('path');

// Define a file path const filePath = '/users/gfg/documents/report.pdf';

// Extract the file extension const fileExtension = path.extname(filePath);

console.log(File Extension: ${fileExtension});

``

**Output:

File Extension: .pdf

**Example: Uses path.resolve() to convert a sequence of path segments into an absolute path.

JavaScript `

console.clear(); const path = require('path'); // Resolving an absolute path from segments const absolutePath = path.resolve('users', 'gfg', 'documents'); console.log(absolutePath);

`

**Output

/your/current/directory/users/gfg/documents

Benefits of Path Module

Also Check: