Node.js path.join() Method Examples

Below examples illustrate the path.join() method in Node.js:

Example 1: In this example we demonstrates the path.join() method. It combines path segments, handling various cases like joining two or three segments, and handling zero-length paths effectively.

Node.js

// Node.js program to demonstrate the   
// path.join() Method  

// Import the path module
const path = require('path');
 
// Joining 2 path-segments
path1 = path.join("users/admin/files", "index.html");
console.log(path1)
 
// Joining 3 path-segments
path2 = path.join("users", "geeks/website", "index.html");
console.log(path2)
 
// Joining with zero-length paths
path3 = path.join("users", "", "", "index.html");
console.log(path3)

Node.js path.join() Method

The path.join() method is used to join a number of path segments using the platform-specific delimiter to form a single path. The final path is normalized after the joining takes place. The path segments are specified using comma-separated values.

Similar Reads

Syntax

path.join( [...paths] )...

Parameters

This function accepts one parameter as mentioned above and described below:...

Return Value

It returns a string with the complete normalized path containing all the segments....

Node.js path.join() Method Examples

Below examples illustrate the path.join() method in Node.js:...