CodeBlanks Logo Icon CodeBlanks

Summary - path module

Combine join, resolve and parse to write a file explorer.

Summary - the path module

In this group you learned the Node.js path module. path.join joins segments, path.resolve makes a path absolute, path.parse splits into five components, dirname and basename give the directory and the name, and extname returns the extension.

These are the core tools for working with file paths safely. The path module handles the OS-specific separator automatically; always use these methods instead of manual string concatenation.

Practice Your turn: join with path.join

Start with import path from 'path'. Write const p = path.join('folder', 'file.txt'). Log it with console.log(p).

Starter code

index.js
// Import the path module
// Join with path.join, log with console.log

Sample solution

index.js
import path from 'path';
const p = path.join('folder', 'file.txt');
console.log(p);

Expected output

folder/file.txt

Practice Your turn: components with path.parse

Start with import path from 'path'. Write const parsed = path.parse('/a/b/c.txt'). Log console.log(parsed.ext).

Starter code

index.js
// Import the path module
// Split with path.parse, log the ext field

Sample solution

index.js
import path from 'path';
const parsed = path.parse('/a/b/c.txt');
console.log(parsed.ext);

Expected output

.txt

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match the path methods

Term Meaning
path.join joins segments
path.resolve makes absolute
path.parse splits into five
path.extname returns extension

2 Which method returns only the extension of a file path?

  • path.join
  • path.dirname
  • path.resolve
  • path.extname Correct answer

Explanation: path.extname returns the extension.

All lessons in Beginner