Path
Thenode:path module provides utilities for working with file and directory paths.
Import
Platform Differences
The default operation varies based on the operating system:- Windows: Uses backslashes (
\) as path separators - POSIX (Linux, macOS): Uses forward slashes (
/)
Cross-Platform Consistency
Core Methods
path.basename(path[, suffix])
Returns the last portion of a path, similar to Unix basename command.
Parameters:
path- The file pathsuffix- Optional suffix to remove
path.dirname(path)
Returns the directory name of a path, similar to Unix dirname command.
Parameters:
path- The file path
path.extname(path)
Returns the file extension from the last occurrence of . to the end of the string.
Parameters:
path- The file path
path.join([...paths])
Joins path segments together using the platform-specific separator, then normalizes the result.
Parameters:
...paths- Path segments to join
path.normalize(path)
Normalizes the given path, resolving '..' and '.' segments.
Parameters:
path- The path to normalize
path.parse(path)
Returns an object with properties representing the path components.
Parameters:
path- The path to parse
root- Root of the pathdir- Directory pathbase- File name including extensionname- File name without extensionext- File extension
path.format(pathObject)
Returns a path string from an object (opposite of path.parse()).
Parameters:
pathObjectdirrootbasenameext
path.isAbsolute(path)
Determines if the path is an absolute path.
Parameters:
path- The path to check
path.relative(from, to)
Returns the relative path from from to to based on the current working directory.
Parameters:
from- Source pathto- Destination path
path.resolve([...paths])
Resolves a sequence of paths or path segments into an absolute path.
Parameters:
...paths- Sequence of paths to resolve
path.matchesGlob(path, pattern)
Determines if a path matches a glob pattern.
Parameters:
path- The path to testpattern- The glob pattern
path.toNamespacedPath(path) (Windows only)
Returns an equivalent namespace-prefixed path (Windows only).
Parameters:
path- The path to convert
Properties
path.sep
Provides the platform-specific path segment separator.
\\on Windows/on POSIX
path.delimiter
Provides the platform-specific path delimiter.
;on Windows:on POSIX
path.posix
Provides access to POSIX-specific implementations of path methods.
path.win32
Provides access to Windows-specific implementations of path methods.
Common Patterns
Building File Paths
Path Manipulation
Resolving Relative Paths
Cross-Platform Path Handling
Best Practices
- Always use
path.join()for combining paths (cross-platform) - Use
path.resolve()to get absolute paths - Avoid string concatenation for paths
- Use
path.sepfor platform-specific separators - Use
path.normalize()to clean up paths - Test on both Windows and POSIX if building cross-platform apps
Common Pitfalls
Windows Path Separators
Relative Path Resolution
See Also
- File System API - File system operations
- OS API - Operating system utilities
- URL - URL parsing and formatting