Node.js – Path Module

Introduction

In this blog post you will learn how to use Path module in Node.js. Node.js Path module is a built-in module used for handling and transforming file paths. The path module provides a lot of useful utilities to access and interact with the file system.

As it is a core module therefore you don’t need to install it. It can be accessed using:

const path = require("path")

Following are some useful method of path module:

path.basename()

This method returns the last portion of a path. Similar to the Unix basename command. A second parameter can filter out the file extension:

path.basename('/tmp/foo') //foo

path.basename('/tmp/foo.txt') //foo.txt

path.basename('/tmp/foo.txt', '.txt') //foo

path.dirname()

This method returns the directory name of a path, similar to the Unix dirname command.

path.dirname('/tmp/foo') // /tmp

path.dirname('/tmp/foo/bar.txt') // /tmp/foo

path.extname()

This method returns the extension of a given path.

path.extname('/tmp/foo') // ''

path.extname('/tmp/foo/bar.txt') // '.txt'

path.isAbsolute()

This method returns true if the give path is an absolute path

path.isAbsolute('/tmp/foo') // true

path.isAbsolute('./tmp/foo') // false

path.resolve()

This method resolve the given path to an absolute path.

path.resolve('foo.txt') // '/Users/sumit/foo.txt' if run from users home folder

By specifying a second parameter, resolve will use the first as a base for the second parameter:

path.resolve('tmp', 'foo.txt') //'/Users/sumit/tmp/foo.txt' if run from users home folder

If the first parameter starts with a slash(/), that means it’s an absolute path:

path.resolve('/tmp', 'foo.txt') //'/tmp/foo.txt'

path.parse()

This method returns an object whose properties represent significant elements of the path. Trailing directory separators are ignored. Returned object components are as follows:

  • root: the root
  • dir: the folder path starting from the root
  • base: file name + extension
  • name: the name of file
  • ext: the file extension
path.parse('/users/foo.txt')

The above code will generate following result:

{
  root: '/',
  dir: '/users',
  base: 'foo.txt',
  ext: '.txt',
  name: 'foo'
}

path.join()

This method joins two or more parts of the path

const name = 'foo'
const users = 'users'

require('path').join('/', users, name, 'bar.txt') //'/users/foo/bar.txt'

Following table is a short description of available methods and properties;

MethodDescription
basename()Returns the last part of a path
relative()Returns the relative path from one specified path to another specified path
dirname()Returns the directories of a path
extname()Returns the file extension of a path
format()Formats a path object into a path string
isAbsolute()Returns true if the given path is an absolute path, otherwise returns false
join()Joins the specified paths into one
normalize()Normalize the specified path
resolve()Resolves the given path into an absolute path
parse()Formats a path string into a path object
posixReturns an object containing POSIX specific properties and methods
delimiterReturns the delimiter specified for the platform
sepReturns the segment separator specified for the platform
win32Returns an object containing Windows specific properties and methods

#node.js path module #path module in node.js #nodejs path module #path module explained #node.js path module explained #path module usage node.js #path module example #how to get absolute path in node.js #Node.js path module #node path module #path.resolve node.js #path.resolve() function node.js #how to get basename of the file in node.js #how to get filename in node.js #how to get filename from a given path in node.js #node.js file module #node.js fs module #use of path module in node.js #what is path module in node js #path module of node js #use of path module in node js #path module in node js #node js path module #how to get last part of given path #how to get file name from the given path #how to get extension of file in node.js #calculate relative path in node.js #how to get absolute path in node.js #how to join two paths in node.js #join two paths using path module node.js

, , ,

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Please disable your adblocker or whitelist this site!

How to whitelist website on AdBlocker?

How to whitelist website on AdBlocker?

  1. 1 Click on the AdBlock Plus icon on the top right corner of your browser
  2. 2 Click on "Enabled on this site" from the AdBlock Plus option
  3. 3 Refresh the page and start browsing the site