NodeJs Path Module
The 'path' module in Node.js is a core module that provides a set of utilities for working with file and directory paths. It helps manage and manipulate file paths in a platform-independent way, meaning you don't have to worry about differences between operating systems like Windows and Linux. The module allows you to build, resolve, and normalize paths easily. This is useful for tasks like constructing file paths, resolving relative paths to absolute paths, and extracting parts of paths.
The path module is built into Node.js, so you don’t need to install anything extra to use it. Just require it at the beginning of your script with:
const path = require('path');
Methods of Path Module
Here’s a comprehensive list of methods provided by the path module in Node.js:
1. path.basename()
The path.basename method in Node.js is used to extract the last portion of a path. This portion is typically the filename in a file path or the last directory in a directory path.
⯌ When your file name is E:/node-tutorials/node/app.js so It will return: app.js
⯌ When your file name is E:/node-tutorials/node so It will return: node
Example
const path = require('path');const fullPath = __filename; //E:\node-tutorials\app.jsconst baseName=path.basename(fullPath); // app.jsconsole.log('Full path of the current file: '+fullPath);console.log('Filename : '+baseName);/*OUTPUTFull path of the current file: E:\node-tutorials\app.jsFilename : app.js*/
With directory
const path = require('path');const currentDir = __dirname; //E:\node-tutorialsconst baseName=path.basename(currentDir); //node-tutorialsconsole.log('Current Directory: '+currentDir);console.log('Basename: '+baseName);/*OUTPUTCurrent Directory: E:\node-tutorialsBasename: node-tutorials*/
2. path.dirname()
This method is used to find the directory name of a given path. Essentially, it strips off the last part of the path and returns the path to the directory containing that last part.
const path = require('path');const dirName =path.dirname("E:/node-tutorials/app.js");console.log('Directory Name: '+dirName);/*OUTPUTDirectory Name: E:/node-tutorials*/
3. path.extname()
This method is used to get the file extension of a given file path. It returns the extension of the file, including the leading dot (.). If the path does not have an extension, it returns an empty string.
const path = require('path');const extName1 =path.extname("E:/node-tutorials/app.js");const extName2 =path.extname("E:/node-tutorials/app");const extName3 =path.extname("E:/node-tutorials/.hiddenfile");console.log('Extension Name1: '+extName1); //Output: '.js'console.log('Extension Name2: '+extName2); //Output: ''console.log('Extension Name3: '+extName3); //Output: ''/*OUTPUTExtension Name1: .jsExtension Name2:Extension Name3:*/
4. path.parse()
This method is used to return an object with properties like root, dir, base, ext, and name for the given path.
const path = require('path');const parsed =path.parse("E:/node-tutorials/app.js");console.log(parsed);/*OUTPUT{root: 'E:/',dir: 'E:/node-tutorials',base: 'app.js',ext: '.js',name: 'app'}*/
You can access each property directly from the parsed object to get the specific part of the path you need. This can be very useful for tasks like modifying file paths, extracting file names or extensions, or building new paths dynamically.
const path = require('path');const parsed =path.parse("E:/node-tutorials/app.js");console.log('Root:', parsed.root); // E:/console.log('Directory:', parsed.dir); // E:/node-tutorialsconsole.log('Base:', parsed.base); // app.jsconsole.log('Extension:', parsed.ext); // .jsconsole.log('Name:', parsed.name); // app/*OUTPUTRoot: E:/Directory: E:/node-tutorialsBase: app.jsExtension: .jsName: app*/
4. path.join()
This method is used to join multiple path segments into a single path. It takes one or more arguments, each representing a segment of the path, and combines them into a single, normalized path.
Example 1: Basic Join
const path = require('path');const result = path.join('E','tutorials' ,'node-tutorials', 'app.js');console.log(result);/*OUTPUTOn Windows:E\tutorials\node-tutorials\app.jsOn Unix-like systems:E/tutorials/node-tutorials/app.js*/
Example 2: Resolving parent directories
Resolving parent directories means finding the path that goes up one or more levels from where you are in the folder structure. So you can use '..' in a file path to go up one level in the directory structure.
const path = require('path');const result = path.join('E','tutorials', '..' ,'node-tutorials', 'app.js');console.log(result);/*OUTPUTE\node-tutorials\app.js*/
The path module in Node.js simplifies working with file and directory paths. It provides tools to join, resolve, and manipulate paths in a cross-platform way, making file operations easier and more reliable.