The Wayback Machine - https://web.archive.org/web/20240118164941/https://www.geeksforgeeks.org/node-js-path-module-complete-reference/
Open In App
Related Articles

Node.js Path Module Complete Reference

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The Path Module in Node.js provides the utilities for working with file and directory paths.

Example:

Javascript




// Import the path module
const path = require('path');
 
// CASE 1
// If "dir", "root" and "base" are all given,
// "root" is ignored.
let path1 = path.format({
    root: "C:\\ignored\\root",
    dir: "website\\dist",
    base: "index.html",
});
console.log("Path 1:", path1);
 
// CASE 2
// If "dir" is not specified then "root"
// will be used
// If only "root" is provided platform
// separator will not be included.
// "ext" will be ignored.
let path2 = path.format({
    root: "C:\\",
    base: "style.css",
    ext: ".ignored",
});
console.log("Path 2:", path2);
 
// CASE 3
// If "base" is not specified
// "name" and "ext" will be used
let path3 = path.format({
    root: "website\\",
    name: "main",
    ext: ".js",
});
console.log("Path 3:", path3);


Output:

Path 1: website\dist\index.html
Path 2: C:\style.css
Path 3: website\main.js

The Complete List of Path are listed below:

Node.js Path Module Methods

Description

Node.js path.basename() Method This is used to get the filename portion of a path to the file. 
Node.js path.dirname() Method This is used to get the directory name of the given path.
Node.js path.extname() Method This is used to get the extension portion of a file path. 
Node.js path.format() Method This is used to return a path string from the given path object.
Node.js path.isAbsolute() Method This is used to check whether the given path is an absolute path or not.
Node.js path.join() Method This is used to join a number of path segments using the platform-specific delimiter to form a single path. 
Node.js path.normalize() Method This is used to normalize the given path.
Node.js path.parse() Method This s used to return an object whose properties represent the given path.
Node.js path.relative() Method This is used to find the relative path from a given path to another path based on the current working directory.
Node.js path.resolve() Method This is used to resolve a sequence of path segments to an absolute path.
Node.js path.toNamespacedPath() Method  This is used to find the equivalent namespace-prefixed path from the given path. 

Node.js Path Module Properties

Description

Node.js path.delimiter Property This is used to get platform-specific path delimiter.

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 07 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials