close
The Wayback Machine - https://web.archive.org/web/20240625110822/https://www.geeksforgeeks.org/node-js-path-join-method/
Open In App

Node.js path.join() Method

Last Updated : 07 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax

path.join( [...paths] )

Parameters

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

  • paths: It is a comma-separated sequence of paths that would be joined together to make the final path.

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:

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)

Output: 

users\admin\files\index.html
users\geeks\website\index.html
users\index.html

Example 2: In this example we use the path.join() method. It normalizes the final path, handles zero-length paths, and retrieves the directory path one folder above the current directory using __dirname.

Node.js

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

// Import the path module
const path = require('path');
 
// Normalizing of the final path
path1 = path.join("users", "..", "files", "readme.md");
console.log(path1)
 
// Zero length final path
// returns a period (.)
path2 = path.join("users", "..");
console.log(path2)
 
// Getting the directory path one folder above
console.log("Current Directory: ", __dirname);
path3 = path.join(__dirname, "..");
console.log("Directory above:", path3)

Output: 

files\readme.md
.
Current Directory: G:\tutorials\nodejs-path-join
Directory above: G:\tutorials

Reference:

https://nodejs.org/api/path.html#path_path_join_paths


Previous Article
Next Article

Similar Reads

Node.js join() function
join() is an array function from Node.js that is used to return a string from the array. Syntax: array_name.join(parameter) Parameter: This function takes a single parameter as per which the join operation has to be performed. Return type: The function returns the string. The program below demonstrates the working of the function: Example 1: C/C++
1 min read
How to Join Two Collections in Mongodb using Node.js ?
Joining two collections in MongoDB using Node.js can be accomplished using the aggregation framework. The $lookup stage in the aggregation pipeline allows you to perform a left outer join to another collection in the same database. Understanding MongoDB CollectionsIn MongoDB, a collection is a group of documents that are stored together within the
4 min read
Convert relative path URL to absolute path URL using JavaScript
Given a relative URL, the task is to convert the relative URL to an absolute URL. Here, the base URL is also given. 2 approaches are discussed here, the first example has the baseURL provided by the user and the second takes it from the URL of the page. Approach 1: Get the relURL and baseURL from user.Use .split() method to split the base and relat
3 min read
JavaScript typedArray.join() Method
The typedArray.join() is an inbuilt function in JavaScript which is used to join all the elements of the given typedArray into a string. Syntax: typedArray.join(separator); Parameters: It accepts a parameter "separator" which is used to separate each element of the typedArray. It is optional and its default value is comma (','). Return value: It re
1 min read
TypeScript | Array join() Method
The Array.join() is an inbuilt TypeScript function which is used to joins all the elements of an array into a string. Syntax: array.join(separator) Parameter: This method accept a single parameter as mentioned above and described below: separator : This parameter is the a string to separate each element of the array. Return Value: This method retur
1 min read
Collect.js join() Method
The join() method is used to join the collection elements with given string and returns the collection elements. Syntax: collect(array).join() Parameters: The collect() method takes one argument that is converted into the collection and then join() method is applied on it. Return Value: This method joins the collection elements with given string an
1 min read
Lodash _.join() Method
Lodash _.join() function is used to convert all elements in the array into a string separated by a separator. Syntax: _.join(array, [separator=',']);Parameter:array: It is the original array from which the join operation is to be performed.separator: A string to separate each element of the array. If leave it by default array element is separated b
2 min read
What is Array.join() method in JavaScript?
In JavaScript, the join() method is used to create and return a new string by concatenating all the elements of an array, separated by a specified separator string. If no separator is provided, a comma (,) is used as the default separator. Syntax:array.join(separator)Parameter:array: The array whose elements will be joined into a string.separator (
1 min read
JavaScript Array join() Method
The JavaScript Array join() Method is used to join the elements of an array into a string. The elements of the string will be separated by a specified separator and its default value is a comma(,). Array join() Method Syntaxarray.join(separator);ParametersThis method accepts a single parameter as mentioned above and described below: separator: It i
2 min read
Node.js path.basename() Method
The path.basename() method is used to get the filename portion of a path to the file. The trailing directory separators are ignored when using this method. Syntax: path.basename( path, extension ) Parameters: This method accepts two parameters as mentioned above and described below: path: It is the file path that would be used to extract the filena
1 min read
three90RightbarBannerImg