Node.js path.join() Method Last Updated : 17 Oct, 2024 Comments Improve Suggest changes Like Article Like Report The path.join() method in Node.js is part of the Path module, which is used for handling and transforming file paths. The method is used to join multiple path segments together into one complete path, ensuring that the resulting path is formatted correctly for the current operating system. In this article, we'll learn more about the path.join() method and explore how it works.What is the path.join() Method?The path.join() method joins all given path segments together using the appropriate platform-specific separator (e.g., a slash / on Linux/macOS and a backslash \ on Windows). It ensures that slashes are placed correctly and removes redundant or unnecessary slashes, making sure that the generated path is valid and usable.This is particularly important when building paths dynamically, for example, when working with directories, file structures, or URLs in cross-platform environments.Syntaxpath.join([path1], [path2], [...])Parameters:path1, path2, ...: These are the path segments that you want to join together. You can provide multiple path segments, and the function will join them correctly.Return Value:It returns a string with the complete normalized path containing all the segments.Node.js path.join() Method ExamplesThe below examples illustrate the path.join() method in Node.js:Example 1: In this example, we demonstrate the path.join() method. It combines path segments, handling various cases like joining two or three segments and handling zero-length paths effectively. JavaScript import path from 'path'; // Joining 2 path-segments let path1 = path.join("users/admin/files", "index.html"); console.log(path1) // Joining 3 path-segments let path2 = path.join("users", "geeks/website", "index.html"); console.log(path2) // Joining with zero-length paths let path3 = path.join("users", "", "", "index.html"); console.log(path3) Output:path.join() method in Node.jsExample 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. JavaScript // 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: Node.js path.join() Method Comment More infoAdvertise with us Next Article Node.js path.normalize() Method sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies Node.js Node.js-path-module Similar Reads 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 1 min read Node.js path.dirname() Method The path.dirname() method is used to get the directory name of the given path. It ignores the respective platform's trailing directory separators. Syntax: path.dirname( path ) Parameters: This function accepts one parameter as mentioned above and described below: path: It is the file path that would 1 min read Node.js path.extname() Method The path.extname() method is used to get the extension portion of a file path. The extension string returned from the last occurrence of a period (.) in the path to the end of the path string. If there are no periods in the file path, then an empty string is returned. Syntax: path.extname( path ) Pa 2 min read Node.js path.format() Method The path.format() method is used to return a path string from the given path object. The method has some rules where one path property gets more priority over another: The "root" parameter of the path object is ignored if the "dir" parameter is provided. The "ext" and "name" parameter of the path ob 3 min read Node.js path.isAbsolute() Method The path.isAbsolute() method is used to check whether the given path is an absolute path or not. An absolute path is defined as a path that contains the complete details needed to locate a file. Syntax: path.isAbsolute( path ) Parameters: This method accepts single parameter path which holds the fil 1 min read Node.js path.join() Method The path.join() method in Node.js is part of the Path module, which is used for handling and transforming file paths. The method is used to join multiple path segments together into one complete path, ensuring that the resulting path is formatted correctly for the current operating system. In this a 2 min read Node.js path.normalize() Method The path.normalize() method is used to normalize the given path. Normalization resolves the (.) and (..) segments of the path to their correct form. If the method encounters multiple path separators, it replaces all of them by a single platform-specific path separator. This method preserves all trai 1 min read Node.js path.parse() Method The path.parse() method is used to return an object whose properties represent the given path. This method returns the following properties: root (root name) dir (directory name) base (filename with extension) ext (only extension) name (only filename) The values of these properties may be different 2 min read Node.js path.relative() Method The path.relative() method is used to find the relative path from a given path to another path based on the current working directory. If both the given paths are the same, it would resolve to a zero-length string. Syntax: path.relative( from, to ) Parameters: This method accept two parameters as me 1 min read Node path.resolve() Method The path.resolve() method takes a sequence of paths or path segments and resolves them into an absolute path. It processes the paths from right to left and appends each path until an absolute path is constructed.Node path.resolve method The path.resolve() method in Node.js helps create a full, absol 3 min read Like