codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🟢

Node.js

11 / 63 topics
6Node.js File System7HTTP Module8Node.js Events9Streams10Buffers11Path Module12OS Module
Tutorials/Node.js/Path Module
🟢Node.js

Path Module

Updated 2026-05-15
10 min read

Path Module

Introduction

In Node.js, the path module provides a way of working with file and directory paths. It is an essential tool for any developer who needs to manipulate or resolve file paths in their applications. The path module offers various methods that help you normalize, join, resolve, and parse file paths across different operating systems.

Concept

The path module provides a set of utilities for working with file and directory paths. These utilities are crucial for tasks such as:

  • Normalization: Ensuring paths are in their canonical form.
  • Joining Paths: Combining multiple path segments into a single path.
  • Resolving Paths: Determining the absolute path of a given relative path.
  • Parsing Paths: Breaking down a path into its components.

Using these utilities, you can write cross-platform code that handles file paths correctly, regardless of the operating system (Windows, macOS, or Linux).

Examples

1. Normalizing Paths

Normalization involves converting a path to its canonical form by resolving references like . and ...

JavaScript
1const path = require('path');
2
3const normalizedPath = path.normalize('/foo/bar//baz/asdf/quux/..');
4console.log(normalizedPath); // Output: /foo/bar/baz/asdf

2. Joining Paths

Joining paths involves combining multiple segments into a single path.

JavaScript
1const path = require('path');
2
3const joinedPath = path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
4console.log(joinedPath); // Output: /foo/bar/baz/asdf

3. Resolving Paths

Resolving paths involves determining the absolute path of a given relative path.

JavaScript
1const path = require('path');
2
3const resolvedPath = path.resolve('foo', 'bar', 'baz');
4console.log(resolvedPath); // Output: /current/working/directory/foo/bar/baz

4. Parsing Paths

Parsing paths involves breaking down a path into its components, such as the root, directory, base, name, and extension.

JavaScript
1const path = require('path');
2
3const parsedPath = path.parse('/foo/bar/baz/asdf/quux.html');
4console.log(parsedPath);
5// Output:
6// {
7// root: '/',
8// dir: '/foo/bar/baz/asdf',
9// base: 'quux.html',
10// name: 'quux',
11// ext: '.html'
12// }

5. Extending Paths

You can also extend paths by adding a new extension or changing the existing one.

JavaScript
1const path = require('path');
2
3const newPath = path.extname('/foo/bar/baz/asdf/quux.js', '.ts');
4console.log(newPath); // Output: /foo/bar/baz/asdf/quux.ts

What's Next?

In the next section, we will explore the OS Module, which provides a way of interacting with the operating system. This module is useful for tasks such as getting information about the current user, the operating system version, and more.

Stay tuned!


PreviousBuffersNext OS Module

Recommended Gear

BuffersOS Module