Lesson 9 of 30

Path Module

Chapter 9

Path Module

Learn how the Node.js path module works with file and directory paths.

Theory

The path module provides utilities for working with file and directory paths. Common methods include: • path.join() • path.resolve() • path.basename() • path.dirname() • path.extname() The path module helps create platform-independent file paths.

Example

Node.js Example
const path = require("path");

const filePath = path.join(
  __dirname,
  "files",
  "data.txt"
);

console.log(filePath);

Output

A correctly constructed file path is displayed.

Practice

Use the path module to create and inspect a file path.

Interview Questions

1.

What is the path module?

2.

What does path.join() do?

3.

What does path.basename() return?

Quick Quiz

Which option best matches this chapter?

A.path
B.fs
C.http
D.crypto

Answer: path