The path module in Node.js is a powerful tool that comes pre-installed with Node.js. It offers a wide range of functionality to easily access and interact with file paths in the file system.

To begin using the path module, simply require it in your code:

const path = require('path');

The path module provides two important properties: path.sep, which represents the path segment separator (\ on Windows and / on Linux/macOS), and path.delimiter, which represents the path delimiter (; on Windows and : on Linux/macOS).

Here are the key methods provided by the path module:

path.basename(): Retrieve the File Name from a Path

The path.basename() method allows you to retrieve the last portion of a given path. You can also provide a second optional parameter to filter out the file extension if required. Here are some examples:

require('path').basename('/test/something'); // Returns: "something"
require('path').basename('/test/something.txt'); // Returns: "something.txt"
require('path').basename('/test/something.txt', '.txt'); // Returns: "something"

path.dirname(): Retrieve the Directory Path from a Path

The path.dirname() method returns the directory part of a given path. It is helpful in getting the directory path excluding the file name. See the examples below:

require('path').dirname('/test/something'); // Returns: "/test"
require('path').dirname('/test/something/file.txt'); // Returns: "/test/something"

path.extname(): Retrieve the File Extension from a Path

Using path.extname(), you can obtain the extension part of a file path. This method will return an empty string if no file extension is present. Observe the examples below:

require('path').extname('/test/something'); // Returns: ""
require('path').extname('/test/something/file.txt'); // Returns: ".txt"

path.isAbsolute(): Check if a Path is Absolute

To determine if a provided path is an absolute path, you can utilize the path.isAbsolute() method. It will return true if the path is an absolute path and false otherwise. Take a look at the following examples:

require('path').isAbsolute('/test/something'); // Returns: true
require('path').isAbsolute('./test/something'); // Returns: false

path.join(): Join Multiple Parts of a Path

The path.join() method is helpful in joining two or more parts of a path. It will automatically handle the correct delimiter based on the underlying operating system. See the example below:

const name = 'flavio';
require('path').join('/', 'users', name, 'notes.txt'); // Returns: "/users/flavio/notes.txt"

path.normalize(): Normalize a Path

The path.normalize() method aids in calculating the actual path when it includes relative specifiers like . or .., or even double slashes. Here’s an example:

require('path').normalize('/users/flavio/..//test.txt'); // Returns: "/users/test.txt"

path.parse(): Parse a Path

The path.parse() method parses a given path and returns an object containing its various components, such as the root, directory path, file name with extension, file name, and file extension. Consider this example:

require('path').parse('/users/test.txt');

The result would be:

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

path.relative(): Get the Relative Path Between Two Paths

path.relative() accepts two paths as arguments and returns the relative path from the first path to the second based on the current working directory. Check out these examples:

require('path').relative('/Users/flavio', '/Users/flavio/test.txt'); // Returns: "test.txt"
require('path').relative('/Users/flavio', '/Users/flavio/something/test.txt'); // Returns: "something/test.txt"

path.resolve(): Resolve an Absolute Path

To obtain the absolute path of a given relative path, you can use path.resolve(). By specifying a second parameter, resolve uses the first as a base for the second. When the first parameter starts with a slash, it indicates an absolute path. Examples:

path.resolve('flavio.txt'); // Returns: "/Users/flavio/flavio.txt"
path.resolve('tmp', 'flavio.txt'); // Returns: "/Users/flavio/tmp/flavio.txt"
path.resolve('/etc', 'flavio.txt'); // Returns: "/etc/flavio.txt"

These are the key methods offered by the Node.js path module. By utilizing this module’s powerful functionality, you can simplify file path operations and seamlessly interact with the file system.