util NodeJS में predefined / core util है इसे use करने के लिए install करने की जरूरत नहीं है। यह module में हमें file and directories के साथ work करने के लिए utilities provide करता है।

Note* अलग - अलग Operating system के according path module अलग - अलग output generate कर सकता है। By Default path module windows operating system के according work करता है।

NodeJS path module Functions

  • path.basename(path[, ext])
  • path.delimiter
  • path.dirname(path)
  • path.extname(path)
  • path.format(pathObject)
  • path.isAbsolute(path)
  • path.join([...paths])
  • path.normalize(path)
  • path.parse(path)
  • path.relative(from, to)
  • path.resolve([...paths])
  • path.sep
  • path.toNamespacedPath(path)
  • path.win32

NodeJS path.basename(path[, ext])

path.basename() method किसी path का last portion return करता है।

Arguments : 
path : string
ext : string (optional file extension.)
returns : string
path.basename() Example
CopyRun
const path = require('path');
console.log(path.basename('C:\\Users\\HP\\Desktop\\workspace\\nodejs\\app.js'));

//Output : app.js

NodeJS path.dirname(path)

path.dirname() method किसी file या directory किस directory में है वो directory path return करता है।

path.basename() Example
CopyRun
const path = require('path');
console.log(path.dirname('C:\\Users\\HP\\Desktop\\workspace\\nodejs\\app.js'));
console.log(path.dirname('C:\\Users\\HP\\Desktop\\workspace\\nodejs'));

//Output : 
C:\Users\HP\Desktop\workspace\nodejs
C:\Users\HP\Desktop\workspace

NodeJS path.parse()

NodeJS path module का parse function किसी path को Object में parse करता है , जिसमे कि नीचे दी गयी properties होती है।

  • root: the root
  • dir: the folder path starting from the root
  • base: the file name + extension
  • name: the file name
  • ext: the file extension
path.parse() Example
Copy Fullscreen Close FullscreenRun
const path = require('path');
let test_path = '/images/learnhindituts_admin.jpg';
console.log(path.parse(test_path));
C:\Users\HP\Desktop\workspace\nodejs>node app.js
{
  root: '/',
  dir: '/images',
  base: 'learnhindituts_admin.jpg',
  ext: '.jpg',
  name: 'learnhindituts_admin'
}

NodeJS extname()

extname() function pass किये गए किसी file path का extension return करता है।

require('path').extname('/test/something') //Outout : ''
require('path').extname('/test/other/file.txt') //Output : '.txt'

NodeJS path.resolve()

resolve()function किसी file का absolute current path return करता है।

require('path').resolve('app.js');
//Output : C:\Users\HP\Desktop\workspace\nodejs\app.js

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers