Skip to content
Newer
Older
100644 157 lines (104 sloc) 3.7 KB
227307e @tj Added node.*.md files
authored
1 # Path
2
3 Stability: 3 - Stable
4
5 This module contains utilities for handling and transforming file
6 paths. Almost all these methods perform only string transformations.
7 The file system is not consulted to check whether paths are valid.
8
9 Use `require('path')` to use this module. The following methods are provided:
10
11 ## path.normalize(p)
12
13 Normalize a string path, taking care of `'..'` and `'.'` parts.
14
15 When multiple slashes are found, they're replaced by a single one;
16 when the path contains a trailing slash, it is preserved.
17 On windows backslashes are used.
18
19 Example:
20
21 path.normalize('/foo/bar//baz/asdf/quux/..')
22 // returns
23 '/foo/bar/baz/asdf'
24
25 ## path.join([path1], [path2], [...])
26
27 Join all arguments together and normalize the resulting path.
28 Non-string arguments are ignored.
29
30 Example:
31
32 path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
33 // returns
34 '/foo/bar/baz/asdf'
35
36 path.join('foo', {}, 'bar')
37 // returns
38 'foo/bar'
39
40 ## path.resolve([from ...], to)
41
42 Resolves `to` to an absolute path.
43
44 If `to` isn't already absolute `from` arguments are prepended in right to left
45 order, until an absolute path is found. If after using all `from` paths still
46 no absolute path is found, the current working directory is used as well. The
47 resulting path is normalized, and trailing slashes are removed unless the path
48 gets resolved to the root directory. Non-string arguments are ignored.
49
50 Another way to think of it is as a sequence of `cd` commands in a shell.
51
52 path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')
53
54 Is similar to:
55
56 cd foo/bar
57 cd /tmp/file/
58 cd ..
59 cd a/../subfile
60 pwd
61
62 The difference is that the different paths don't need to exist and may also be
63 files.
64
65 Examples:
66
67 path.resolve('/foo/bar', './baz')
68 // returns
69 '/foo/bar/baz'
70
71 path.resolve('/foo/bar', '/tmp/file/')
72 // returns
73 '/tmp/file'
74
75 path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif')
76 // if currently in /home/myself/node, it returns
77 '/home/myself/node/wwwroot/static_files/gif/image.gif'
78
79 ## path.relative(from, to)
80
81 Solve the relative path from `from` to `to`.
82
83 At times we have two absolute paths, and we need to derive the relative
84 path from one to the other. This is actually the reverse transform of
85 `path.resolve`, which means we see that:
86
87 path.resolve(from, path.relative(from, to)) == path.resolve(to)
88
89 Examples:
90
91 path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb')
92 // returns
93 '..\\..\\impl\\bbb'
94
95 path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb')
96 // returns
97 '../../impl/bbb'
98
99 ## path.dirname(p)
100
101 Return the directory name of a path. Similar to the Unix `dirname` command.
102
103 Example:
104
105 path.dirname('/foo/bar/baz/asdf/quux')
106 // returns
107 '/foo/bar/baz/asdf'
108
109 ## path.basename(p, [ext])
110
111 Return the last portion of a path. Similar to the Unix `basename` command.
112
113 Example:
114
115 path.basename('/foo/bar/baz/asdf/quux.html')
116 // returns
117 'quux.html'
118
119 path.basename('/foo/bar/baz/asdf/quux.html', '.html')
120 // returns
121 'quux'
122
123 ## path.extname(p)
124
125 Return the extension of the path, from the last '.' to end of string
126 in the last portion of the path. If there is no '.' in the last portion
127 of the path or the first character of it is '.', then it returns
128 an empty string. Examples:
129
130 path.extname('index.html')
131 // returns
132 '.html'
133
134 path.extname('index.')
135 // returns
136 '.'
137
138 path.extname('index')
139 // returns
140 ''
141
142 ## path.sep
143
144 The platform-specific file separator. `'\\'` or `'/'`.
145
146 An example on linux:
147
148 'foo/bar/baz'.split(path.sep)
149 // returns
150 ['foo', 'bar', 'baz']
151
152 An example on windows:
153
154 'foo\\bar\\baz'.split(path.sep)
155 // returns
156 ['foo', 'bar', 'baz']
Something went wrong with that request. Please try again.