What is the NodeJS Path Module?

Path Module Featured Image

NodeJS path module is a core built-in module. It provides functionalities for accessing and interacting with files. It provides users a way of working with file paths and directories.

Since it is a core module, you do not need to install it. However, to include the path module in your application you must ‘require’ it.

Importing NodeJS Path Module

const path = require('path');

We will be using this variable throughout this documentation to keep our code compact.

NodeJS Path Properties & Methods

The Node.js path module comes with multiple properties and modules to let developers easily perform tasks. Please note that the arguments surrounded by [ ] (two square brackets) mean they are optional. You may pass them depending upon your requirement.

1. basename(path, [ext])

Returns the last part of a file path. A second parameter will hold the file extension.

path.basename('/random/random') //random
path.basename('/random/random.txt') //random.txt
path.basename('/random/random.txt', '.txt') //random

2. delimiter

Returns the delimiter for the path. Windows uses the ‘;’ (semicolon) delimiter and Linux and MacOS use the ‘:’ (colon) delimiter.

console.log(path.delimiter);

3. dirname(path)

Returns the directories of a path.

path.dirname('/random/something') // /random
path.dirname('/random/something/file.txt') // /random/something

4. extname(path)

Returns the file extension of a path.

path.extname('/random/test') // ''
path.extname('/random/test/file.txt') // '.txt'

5. format(pathObject)

Returns path string from a path object. It is the opposite of path.parse(). It accepts keys or properties such as:

As a rule of thumb, the root key is ignored in case dir is present. Similarly, ext and name keys are ignored if the base is provided.

// POSIX
path.format({ dir: '/Users/Cassandra', base: 'file.txt' }) //  '/Users/Cassandra/file.txt'

path.format({ root: '/Users/Cassandra', name: 'file', ext: 'txt' }) //  '/Users/Cassandra/file.txt'

// WINDOWS
path.format({ dir: 'C:\\Users\\Cassandra', base: 'file.txt' }) //  'C:\\Users\\Cassandra\\file.txt'

6. isAbsolute(path)

Checks if a file is an absolute path or not. Returns true if it is, otherwise, false.

path.isAbsolute('/random/test) // true
path.isAbsolute('./random/test) // false

7. join([…paths])

Joins multiple paths into one.

const name = 'Cassandra'
path.join('/', 'Users', name, 'test.txt') //'/Users/Cassandra/test.txt'

8. normalize(path)

Tries to estimate the actual path when relative specifiers like ‘.’ or ‘..’, double slashes are present.

path.normalize('/Users/Cassandra/..//list.txt') //'/Users/list.txt'

9. parse(path)

Formats the path string into a path object with the properties that it comprises:

  • root: the root of the path
  • dir: the directory path starting from the root
  • base: the file name and extension
  • name: the file name
  • ext: the file extension of the path

We are required to pass the path string:

path.parse('/Users/list.txt')

The result:

{
  root: '/',
  dir: '/Users',
  base: 'list.txt',
  ext: '.txt',
  name: 'list'
}

10. posix

Returns an object that contains POSIX-specific properties and methods. This property enables users to access POSIX-specific implementations of the path methods.

console.log(path.posix);

11. relative(from, to)

Returns a relative path from the first path to the second, based on the current working directory.

path.relative('/Users/Cassandra', '/Users/Cassandra/test.txt') //'test.txt'
path.relative('/Users/Cassandra ', '/Users/Cassandra/something/test.txt') //'something/test.txt'

12. resolve([…paths])

Returns an absolute path estimate of a relative path(s).

path.resolve('list.txt') //'/Users/Cassandra/list.txt'

13. sep

Returns the platform-specific segment separator of the path.

  • ‘\’ on Windows
  • ‘/’ on POSIX

14. win32

Returns an object that contains Windows-specific properties and methods. This property enables users to access Windows-specific implementations of the path methods.

console.log(path.win32);

Conclusion

All in all, the Nodejs path module helps developers handle and manipulate file paths. Most of the path methods and string manipulations.

You might as well want to read: How to Check if a File exists in Node.js

References: Node.js path API docs