How to Use Path Module in Node.js: Syntax and Examples
In Node.js, use the
path module by importing it with import path from 'path' or const path = require('path'). It provides methods like path.join() and path.resolve() to handle and manipulate file paths safely across different operating systems.Syntax
The path module is built into Node.js and is used by importing it first. Key methods include:
path.join(...paths): Joins multiple path segments into one path.path.resolve(...paths): Resolves a sequence of paths into an absolute path.path.basename(path): Returns the last part of a path (file or folder name).path.dirname(path): Returns the directory part of a path.path.extname(path): Returns the file extension.
javascript
import path from 'path'; // Join paths const fullPath = path.join('folder', 'subfolder', 'file.txt'); // Resolve to absolute path const absPath = path.resolve('folder', 'file.txt'); // Get file name const fileName = path.basename(fullPath); // Get directory name const dirName = path.dirname(fullPath); // Get file extension const ext = path.extname(fullPath);
Example
This example shows how to join folder names and a file name into a single path, then get the file name and extension from it.
javascript
import path from 'path'; const folder = 'documents'; const subfolder = 'images'; const file = 'photo.png'; const fullPath = path.join(folder, subfolder, file); console.log('Full Path:', fullPath); const fileName = path.basename(fullPath); console.log('File Name:', fileName); const extension = path.extname(fullPath); console.log('Extension:', extension);
Output
Full Path: documents/images/photo.png
File Name: photo.png
Extension: .png
Common Pitfalls
Common mistakes when using the path module include:
- Using string concatenation instead of
path.join(), which can cause wrong paths on different OS (Windows uses backslashes, Unix uses slashes). - Not resolving relative paths to absolute paths when needed, causing errors in file access.
- Confusing
path.join()andpath.resolve():joinjust concatenates,resolvecalculates an absolute path.
javascript
import path from 'path'; // Wrong: string concatenation (may break on Windows) const wrongPath = 'folder' + '/' + 'file.txt'; // Right: use path.join const rightPath = path.join('folder', 'file.txt'); console.log('Wrong Path:', wrongPath); console.log('Right Path:', rightPath);
Output
Wrong Path: folder/file.txt
Right Path: folder\file.txt
Quick Reference
| Method | Description | Example |
|---|---|---|
| path.join(...paths) | Joins multiple path segments | path.join('a', 'b', 'c') → 'a/b/c' |
| path.resolve(...paths) | Resolves to absolute path | path.resolve('a', 'b') → '/current/dir/a/b' |
| path.basename(path) | Returns last part of path | path.basename('/a/b/c.txt') → 'c.txt' |
| path.dirname(path) | Returns directory part | path.dirname('/a/b/c.txt') → '/a/b' |
| path.extname(path) | Returns file extension | path.extname('file.txt') → '.txt' |
Key Takeaways
Always import the path module using import or require before using it.
Use path.join() to safely combine path segments across different operating systems.
Use path.resolve() to get absolute paths from relative ones.
Avoid manual string concatenation for paths to prevent errors.
Use path.basename(), path.dirname(), and path.extname() to extract parts of a path.