NodeJS Path Module

NodeJS Path Module

MEAN Stack 23-09-2021 Saheb Sutradhar

NodeJS Path Module

NodeJS path module is the builtin module of nodeJS ,We use the path module to work with the file path, By requiring the path module using require function we can directly import the path module in the our project. The module returns an object that we are storing in the path constant.

const path = require('path');

Properties and Methods of the path module


Returns the last path / filename form the path

const path = require('path');
let basename = path.basename('./middleware/login.js')

//Output - login.js



Joints all the arguments together and create a path.

const path = require('path');
let fullPath = path.join('','/', 'tutorial','/', 'path.js');

//Output -



returns the extension of the file

const path = require('path');
let extname = path.extname('./middleware/login.js');

//Output - .js



parse the string path and parse it into a path object

const path = require('path');
let parsedURL = path.parse('./middleware/login.js');

/*Output - 
  root: '',
  dir: './middleware',
  base: 'login.js',
  ext: '.js',
  name: 'login'



returns the directory name of the file

const path = require('path');
let dirname = path.dirname('./middleware/login.js');

//Output - ./middleware



format() method helps to format the path object into a string and returns the result.

const path = require('path');
let pathObj = { dir: './middleware', base: 'login.js' }
let pathResult = path.format(pathObj);

//Output - ./middleware/login.js



Normalize the path and handles '..' and '.' in the path

const path = require('path');
let fixPath = path.normalize('Saheb/Tutorial/../json');

//Output - Saheb/json



Learn the module system in nodejs


I hope this article is helpful for you , i am very glad to help you thanks for reading


Related Posts

Card image cap

Express Middleware

MEAN Stack 24-09-2021 Saheb Sutradhar

Middleware is a function which takes request object , either it returns response to the client or passes the control to the next middleware. ...

Card image cap

What is JWT and Why should we use JWT

MEAN Stack 01-10-2021 Saheb Sutradhar

What is JWT and Why should we use JWT , JWT stands for json wen token , JWT we use for authorization not for authentication . ...