0
# Path Resolution
1
2
Core path resolution functionality for converting relative paths to absolute paths, normalizing path structures, and determining path types.
3
4
## Capabilities
5
6
### Resolve
7
8
Resolves a sequence of paths or path segments into an absolute path. The path is built by processing each path segment from right to left, prepending each subsequent path until an absolute path is constructed.
9
10
```javascript { .api }
11
/**
12
* Resolves a sequence of paths to an absolute path
13
* @param {...string} paths - Path segments to resolve (processed right to left)
14
* @returns {string} Absolute path resolved from current working directory
15
* @throws {TypeError} If any argument is not a string
16
*/
17
function resolve(...paths: string[]): string;
18
```
19
20
**Usage Examples:**
21
22
```javascript
23
const path = require("path");
24
25
// Resolve from current working directory
26
path.resolve("docs", "readme.md");
27
// If cwd is /home/user: "/home/user/docs/readme.md"
28
29
// Resolve with absolute path segment
30
path.resolve("/users", "john", "../jane", "file.txt");
31
// Result: "/users/jane/file.txt"
32
33
// Multiple relative segments
34
path.resolve("..", "src", "index.js");
35
// Resolves relative to parent directory
36
37
// Empty path defaults to current directory
38
path.resolve("");
39
// Same as process.cwd()
40
```
41
42
**Platform Differences:**
43
44
```javascript
45
// Windows
46
path.win32.resolve("C:", "temp", "file.txt");
47
// Result: "C:\\temp\\file.txt"
48
49
// POSIX
50
path.posix.resolve("/usr", "local", "bin");
51
// Result: "/usr/local/bin"
52
```
53
54
### Normalize
55
56
Normalizes a path by resolving `..` and `.` segments. When multiple path separators are found, they are replaced with a single separator. Trailing separators are preserved.
57
58
```javascript { .api }
59
/**
60
* Normalizes a path by resolving . and .. segments
61
* @param {string} path - Path to normalize
62
* @returns {string} Normalized path
63
*/
64
function normalize(path: string): string;
65
```
66
67
**Usage Examples:**
68
69
```javascript
70
const path = require("path");
71
72
// Remove redundant separators and resolve . and ..
73
path.normalize("/users//john/../jane/./docs");
74
// Result: "/users/jane/docs"
75
76
path.normalize("../docs/./readme.md");
77
// Result: "../docs/readme.md"
78
79
// Preserve trailing slash
80
path.normalize("/users/john/");
81
// Result: "/users/john/"
82
83
// Handle empty path
84
path.normalize("");
85
// Result: "."
86
```
87
88
**Platform Differences:**
89
90
```javascript
91
// Windows normalization
92
path.win32.normalize("C:/users\\john/..\\jane");
93
// Result: "C:\\users\\jane"
94
95
// POSIX normalization
96
path.posix.normalize("/users//john/../jane");
97
// Result: "/users/jane"
98
```
99
100
### Is Absolute
101
102
Determines if a path is absolute. On POSIX systems, a path is absolute if it starts with `/`. On Windows, a path is absolute if it starts with a drive letter and colon, or a UNC path.
103
104
```javascript { .api }
105
/**
106
* Determines if a path is absolute
107
* @param {string} path - Path to test
108
* @returns {boolean} True if path is absolute
109
*/
110
function isAbsolute(path: string): boolean;
111
```
112
113
**Usage Examples:**
114
115
```javascript
116
const path = require("path");
117
118
// POSIX absolute paths
119
path.posix.isAbsolute("/home/user"); // true
120
path.posix.isAbsolute("home/user"); // false
121
path.posix.isAbsolute("../docs"); // false
122
123
// Windows absolute paths
124
path.win32.isAbsolute("C:\\Users\\John"); // true
125
path.win32.isAbsolute("\\\\server\\share"); // true (UNC path)
126
path.win32.isAbsolute("Users\\John"); // false
127
path.win32.isAbsolute("C:file.txt"); // false (drive-relative)
128
```
129
130
**Cross-Platform Testing:**
131
132
```javascript
133
// Test on current platform
134
path.isAbsolute("/usr/local");
135
// true on POSIX, false on Windows
136
137
path.isAbsolute("C:\\Windows");
138
// true on Windows, false on POSIX
139
140
// Force platform-specific behavior
141
path.posix.isAbsolute("C:\\Windows"); // false
142
path.win32.isAbsolute("/usr/local"); // false
143
```