Best Online Tutorials

SQL Tutorial

Free SQL Tutorial

SQL Interview Questions

SQL Practice Questions

SQL Tips & Tricks

SQL Errors

SQL Books

Angular Tutorial

Free Angular Tutorial

Angular Interview Questions

Angular Practice Questions

Angular Tips & Tricks

Angular Errors

Angular Books

Node.js Tutorial

Free Node.js Tutorial

Node.js Interview Questions

Node.js Practice Questions

Node.js Tips & Tricks

Node.js Errors

Node.js Books

React.js Tutorial

Free React.js Tutorial

React.js Interview Questions

React.js Practice Questions

React.js Tips & Tricks

React.js Errors

React.js Books

SSIS Tutorial

Free SSIS Tutorial

SSIS Interview Questions

SSIS Practice Questions

SSIS Tips & Tricks

SSIS Errors

SSIS Books

SEO Tutorial

Free SEO Tutorial

SEO Interview Questions

SEO Practice Questions

SEO Tips & Tricks

SEO Errors

SEO Books

Javascript Tutorial

Free Javascript Tutorial

Javascript Interview Questions

Javascript Practice Questions

Javascript Tips & Tricks

Javascript Errors

Javascript Books

MongoDB Tutorial

Free MongoDB Tutorial

MongoDB Interview Questions

MongoDB Practice Questions

MongoDB Tips & Tricks

MongoDB Errors

MongoDB Books

Next.js Tutorial

Free Next.js Tutorial

Next.js Interview Questions

Next.js Practice Questions

Next.js Tips & Tricks

Next.js Errors

Next.js Books

Node.js - Path 

Prashant Surya  |   Nov 19, 2019  |  Views: 8

Description:

path is a module in node.js which is responsible for providing the utilities to work around files and directories.

Windows and POSIX (Portable Operating System Interface) supports different path styles.
So using path.basename() might return different results on POSIX and Windows.

Windows:
path.basename('C:\\demo\\test.txt');
// Output: 'test.txt'


POSIX:
path.basename('C:\\demo\\test.txt');
// Output: 'C:\\demo\\test.txt'


To get same Output for both POSIX and Windows:
path.win32.basename('C:\\demo\\test.txt');
// Output: 'test.txt'



Path is the most important module in Node.js which you should know very well before playing with node.js programming.

Path class exposes following methods and properties, which we will discuss:

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.posix
path.relative(from, to)
path.resolve([...paths])
path.sep
path.toNamespacedPath(path)
path.win32




1.  path.basename(path[, ext]):

It returns the last portion of a given path.
If the second argument 'ext' is passed then the returned portion will exclude the extension provided in argument, return ing only the file name without extension.

Example:
path.basename('/node/demo/test.txt');
// Output: 'test.txt'
path.basename('/node/demo/test.txt', '.txt');
// Output: 'test'




2.  path.delimiter:

A path seperator which is different for both Windows and POSIX:
For Windows delimiter is ';'
For POSIX delimiter is ':'


Example with Windows:
console.log(process.env.PATH);
// Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'
process.env.PATH.split(path.delimiter);
// Output ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']

Example with POSIX:
console.log(process.env.PATH);
// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'
process.env.PATH.split(path.delimiter);
// Output: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']



3.  path.dirname(path):

It return the directory name of a given path.

Example:
path.dirname('/node/demo/test.txt');
// Output: '/node/demo'




4.  path.extname(path):

It return the extension of the given path.

Example:
path.extname('/node/demo/test.txt');
// Returns: '.txt'
path.extname('/node/demo/test.1.txt');
// Returns: '.txt'
path.extname('/node/demo/test.');
// Returns: '.'
path.extname('/node/demo/test');
// Returns: ''
path.extname('/node/demo/.test');
// Returns: '' 




5.  path.format(pathObject):

It is used for the correct formatting of given pathObject properties and return in a single path combing all given input parameters in a string. 

It is the opposite of path.parse(path).

pathObject has following properties:
dir <string>
root <string>
base <string>
name <string>
ext <string>

Example:

path.format({
  root: '/ignored',
  dir: '/demo/dritalconnect',
  base: 'test.txt',
  name: 'test'
  ext: 'ignored'
});
// Output: '/demo/dritalconnect/test.txt'


Note:
If root and dir both are passed then, root will be ignored.
If dir is not specified, root will be considered.
If only root is provided or dir is equal to root then the platform separator will not be included also ext will be ignored.
If base is not passed then name + ext will be considered as base.



6.  path.isAbsolute(path)

It returns true/false, checking if the given path is an absolute path or not.
If path string is of zero-length, it will return false.

Example :

For POSIX:
path.isAbsolute('/demo/dritalconnect');    // true
path.isAbsolute('/demo/..');               // true
path.isAbsolute('demo/');                  // false
path.isAbsolute('.');                      // false

For Windows:
path.isAbsolute('//demo');                  // true
path.isAbsolute('\\\\demo');                // true
path.isAbsolute('C:/demo/..');              // true
path.isAbsolute('C:\\demo\\..');            // true
path.isAbsolute('dritalconnect\\demo');     // false
path.isAbsolute('dritalconnect/demo');      // false
path.isAbsolute('.');                       // false



7.  path.join([...paths])
It accepts the array of strings and return by joining them as an absoulte path.


Note:
Zero-length passed path segments are ignored.
If the joined path is of Zero-length then it will return (.) which shows the current directory.

Example:
path.join('/demo', 'dritalconnect', 'nodejs/tutorial', 'path', '..');
// Output: '/demo/dritalconnect/nodejs/tutorial'


8.  path.parse(path):

It return the path object hvaing following elements-
dir <string>
root <string>
base <string>
name <string>
ext <string> 

Example:
path.parse('/home/user/dir/file.txt');
// Output:

{ root: '/',
dir: '/home/user/dir',
base: 'file.txt',
ext: '.txt',
name: 'file' 
}


9.  path.posix
It provides access to the POSIX specific implementations of the path methods.


10. path.relative(from, to);

It return the relative path of the given 'to' path from the given 'from' path.

Example:
path.relative('C:\\demo\\dritalconnect\\tutorial', 'C:\\demo\\dritalconnect\\assets\\img');
// Returns: '..\\..\\assets\\img'


11.  path.resolve([...paths]):
It resolves a sequence of paths or path segments into an absolute path.
A Zero-length path segments are ignored.

Example:
path.resolve('/demo/dritalconnect', './test');
// Returns: '/demo/dritalconnect/test'

path.resolve('/demo/dritalconnect', '/test/file/');
// Returns: '/test/file'

path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
// If the current working directory is /demo/dritalconnect then it returns '/demo/dritalconnect/wwwroot/static_files/gif/image.gif' as an absolute path.



12. path.sep

It provides the path segment seperator which will be platorm specific.
For Windows result will be '\'
For POSIX result will be '/'


Example :

POSIX:

'demo/dritalconnect/tutorial'.split(path.sep);
// Output: ['demo', 'dritalConnect', 'tutorial']

Windows:

'demo\\dritalconnect\\tutorial'.split(path.sep);
// Output: ['demo', 'dritalConnect', 'tutorial']


13.  path.toNamespacedPath(path)

It works only for windows.
It returns an equivalent namespace-prefixed path for the given path.
If it is used on POSIX , it will return the unmodified path.


14.  path.win32

It provides access to Windows-specific implementations of the path methods.

Example:

path.win32.basename('C:\\demo\\test.txt');
// Output: 'test.txt'



So, you have learnt all the elements of the path Class, when to use and how to use.




Comments:
Content Contributor
Prashant Surya

Prashant Surya

Software developer at Uniserve Data Technologies Pvt. Ltd.  |   Noida, Uttar Pradesh, India

I am a software developer, freelancer and trainer working on technologies likes. MEAN stack, MERN Stack, ASP.NET full stack, WebRTC, socket.io ,PWA, SQL, JavaScript, jQuery, HTML, CSS, SEO, responsive UI/UX designing... having 7 years of experience in development in multiple domains like, eCommerce, RTA, capital markets, education, real estate...


Read related articles

Node.js - Modules

Prashant Surya  |   Dec 26, 2019  |  Views: 10

Node.js - Cluster

Prashant Surya  |   Nov 19, 2019  |  Views: 12

Node.js - IPC

Prashant Surya  |   Nov 19, 2019  |  Views: 9

Node.js - OS

Prashant Surya  |   Nov 19, 2019  |  Views: 7

Node.js - Global Objects

Prashant Surya  |   Nov 19, 2019  |  Views: 13

Node.js - Command Line Options

Prashant Surya  |   Nov 19, 2019  |  Views: 19

Node.js - Callbacks

Prashant Surya  |   Nov 19, 2019  |  Views: 11

Node.js - NPM

Prashant Surya  |   Nov 19, 2019  |  Views: 8

Node.js - REPL

Prashant Surya  |   Nov 19, 2019  |  Views: 9

Node.js - Console

Prashant Surya  |   Nov 19, 2019  |  Views: 8

Node.js - First Example

Prashant Surya  |   Nov 19, 2019  |  Views: 13

Node.js - Installation

Prashant Surya  |   Nov 19, 2019  |  Views: 9

Node.js - Introduction

Prashant Surya  |   Nov 19, 2019  |  Views: 17

Node.js - Http

Prashant Surya  |   Nov 19, 2019  |  Views: 11

Node.js - Timer

Prashant Surya  |   Nov 19, 2019  |  Views: 15

Node.js - Crypto

Prashant Surya  |   Nov 19, 2019  |  Views: 10

Node.js - Errors

Prashant Surya  |   Nov 19, 2019  |  Views: 13

Node.js - File System

Prashant Surya  |   Nov 19, 2019  |  Views: 9

Node.js - Http/2

Prashant Surya  |   Nov 19, 2019  |  Views: 9

Node.js - Query String

Prashant Surya  |   Nov 19, 2019  |  Views: 8

Node.js - Async Hooks

Prashant Surya  |   Nov 19, 2019  |  Views: 16

Node.js - Https

Prashant Surya  |   Nov 19, 2019  |  Views: 8

Node.js - String Decoder

Prashant Surya  |   Nov 19, 2019  |  Views: 12

Node.js - DNS

Prashant Surya  |   Nov 19, 2019  |  Views: 8

Node.js - Debugger

Prashant Surya  |   Nov 19, 2019  |  Views: 9

Node.js - TLS/SSL

Prashant Surya  |   Nov 19, 2019  |  Views: 12

Node.js - Child Process

Prashant Surya  |   Nov 19, 2019  |  Views: 13

Node.js - Process

Prashant Surya  |   Nov 19, 2019  |  Views: 9

Node.js - Streams

Prashant Surya  |   Nov 19, 2019  |  Views: 13

Node.js - Net

Prashant Surya  |   Nov 19, 2019  |  Views: 7

Node.js - Crypto

Prashant Surya  |   Nov 19, 2019  |  Views: 12

Node.js - Performance Hooks

Prashant Surya  |   Nov 19, 2019  |  Views: 8

Node.js - Path

Prashant Surya  |   Nov 19, 2019  |  Views: 9

Node.js - StringDecoder

Prashant Surya  |   Nov 19, 2019  |  Views: 17

Node.js - Assertion

Prashant Surya  |   Nov 19, 2019  |  Views: 6

Node.js - Query String

Pooja Goel  |   Nov 19, 2019  |  Views: 7

Node.js - Callbacks

Prashant Surya  |   Nov 19, 2019  |  Views: 16

Node.js - Buffers

Prashant Surya  |   Nov 19, 2019  |  Views: 11

Node.js - Events

Prashant Surya  |   Nov 19, 2019  |  Views: 18

Node.js - ZLIB

Prashant Surya  |   Nov 19, 2019  |  Views: 13

Node.js - TTY

Prashant Surya  |   Nov 19, 2019  |  Views: 12

Node.js - Trace Events

Prashant Surya  |   Nov 19, 2019  |  Views: 5

Node.js - Worker Threads

Prashant Surya  |   Nov 19, 2019  |  Views: 8

Node.js - Web Modules

Prashant Surya  |   Nov 19, 2019  |  Views: 8

Want to sell your software ?-

If you want to sell your readymade software to the genuine clients or businessman, list your software with details and demo links.

  • Sell ERP
  • Sell Website Clones
  • Sell App Clones
  • Sell Readymade Portals
  • Sell Readymade Websites
  • Sell Mobile Apps
  • Sell Window Apps

Clients will find it using our advanced search filter and will contact you directly.

No any charge for the product lsiting.

Do you own a Company ?+

Property.sale

Buy or Sell Commercial Property+

Are you a freelancer ?+

Looking for a job ?+

Are you a Service Provider ?+

Are you a blogger ?+

Do you own an IT Institute ?+



Property.sale