1<!DOCTYPE html> 2<html lang="en"> 3<head> 4 <meta charset="utf-8"> 5 <meta name="viewport" content="width=device-width"> 6 <meta name="nodejs.org:node-version" content="v14.21.2"> 7 <title>Path | Node.js v14.21.2 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/path.html"> 12</head> 13<body class="alt apidoc" id="api-section-path"> 14 <div id="content" class="clearfix"> 15 <div id="column2" class="interior"> 16 <div id="intro" class="interior"> 17 <a href="/" title="Go back to the home page"> 18 Node.js 19 </a> 20 </div> 21 <ul> 22<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> 23<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> 24</ul> 25<hr class="line"> 26<ul> 27<li><a href="assert.html" class="nav-assert">Assertion testing</a></li> 28<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> 29<li><a href="buffer.html" class="nav-buffer">Buffer</a></li> 30<li><a href="addons.html" class="nav-addons">C++ addons</a></li> 31<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> 32<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> 33<li><a href="child_process.html" class="nav-child_process">Child processes</a></li> 34<li><a href="cluster.html" class="nav-cluster">Cluster</a></li> 35<li><a href="cli.html" class="nav-cli">Command-line options</a></li> 36<li><a href="console.html" class="nav-console">Console</a></li> 37<li><a href="corepack.html" class="nav-corepack">Corepack</a></li> 38<li><a href="crypto.html" class="nav-crypto">Crypto</a></li> 39<li><a href="debugger.html" class="nav-debugger">Debugger</a></li> 40<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> 41<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> 42<li><a href="dns.html" class="nav-dns">DNS</a></li> 43<li><a href="domain.html" class="nav-domain">Domain</a></li> 44<li><a href="errors.html" class="nav-errors">Errors</a></li> 45<li><a href="events.html" class="nav-events">Events</a></li> 46<li><a href="fs.html" class="nav-fs">File system</a></li> 47<li><a href="globals.html" class="nav-globals">Globals</a></li> 48<li><a href="http.html" class="nav-http">HTTP</a></li> 49<li><a href="http2.html" class="nav-http2">HTTP/2</a></li> 50<li><a href="https.html" class="nav-https">HTTPS</a></li> 51<li><a href="inspector.html" class="nav-inspector">Inspector</a></li> 52<li><a href="intl.html" class="nav-intl">Internationalization</a></li> 53<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> 54<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> 55<li><a href="module.html" class="nav-module">Modules: <code>module</code> API</a></li> 56<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> 57<li><a href="net.html" class="nav-net">Net</a></li> 58<li><a href="os.html" class="nav-os">OS</a></li> 59<li><a href="path.html" class="nav-path active">Path</a></li> 60<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> 61<li><a href="policy.html" class="nav-policy">Policies</a></li> 62<li><a href="process.html" class="nav-process">Process</a></li> 63<li><a href="punycode.html" class="nav-punycode">Punycode</a></li> 64<li><a href="querystring.html" class="nav-querystring">Query strings</a></li> 65<li><a href="readline.html" class="nav-readline">Readline</a></li> 66<li><a href="repl.html" class="nav-repl">REPL</a></li> 67<li><a href="report.html" class="nav-report">Report</a></li> 68<li><a href="stream.html" class="nav-stream">Stream</a></li> 69<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> 70<li><a href="timers.html" class="nav-timers">Timers</a></li> 71<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> 72<li><a href="tracing.html" class="nav-tracing">Trace events</a></li> 73<li><a href="tty.html" class="nav-tty">TTY</a></li> 74<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> 75<li><a href="url.html" class="nav-url">URL</a></li> 76<li><a href="util.html" class="nav-util">Utilities</a></li> 77<li><a href="v8.html" class="nav-v8">V8</a></li> 78<li><a href="vm.html" class="nav-vm">VM</a></li> 79<li><a href="wasi.html" class="nav-wasi">WASI</a></li> 80<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> 81<li><a href="zlib.html" class="nav-zlib">Zlib</a></li> 82</ul> 83<hr class="line"> 84<ul> 85<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> 86</ul> 87 </div> 88 89 <div id="column1" data-id="path" class="interior"> 90 <header> 91 <div class="header-container"> 92 <h1>Node.js v14.21.2 documentation</h1> 93 <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> 94 <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> 95 <path fill="none" d="M0 0h24v24H0z" /> 96 <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> 97 <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> 98 </svg> 99 <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> 100 <path d="M0 0h24v24H0z" fill="none" /> 101 <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> 102 </svg> 103 </button> 104 </div> 105 <div id="gtoc"> 106 <ul> 107 <li> 108 <a href="index.html">Index</a> 109 </li> 110 <li> 111 <a href="all.html">View on single page</a> 112 </li> 113 <li> 114 <a href="path.json">View as JSON</a> 115 </li> 116 117 <li class="version-picker"> 118 <a href="#">View another version <span>▼</span></a> 119 <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v19.x/api/path.html">19.x</a></li> 120<li><a href="https://nodejs.org/docs/latest-v18.x/api/path.html">18.x <b>LTS</b></a></li> 121<li><a href="https://nodejs.org/docs/latest-v17.x/api/path.html">17.x</a></li> 122<li><a href="https://nodejs.org/docs/latest-v16.x/api/path.html">16.x <b>LTS</b></a></li> 123<li><a href="https://nodejs.org/docs/latest-v15.x/api/path.html">15.x</a></li> 124<li><a href="https://nodejs.org/docs/latest-v14.x/api/path.html">14.x <b>LTS</b></a></li> 125<li><a href="https://nodejs.org/docs/latest-v13.x/api/path.html">13.x</a></li> 126<li><a href="https://nodejs.org/docs/latest-v12.x/api/path.html">12.x</a></li> 127<li><a href="https://nodejs.org/docs/latest-v11.x/api/path.html">11.x</a></li> 128<li><a href="https://nodejs.org/docs/latest-v10.x/api/path.html">10.x</a></li> 129<li><a href="https://nodejs.org/docs/latest-v9.x/api/path.html">9.x</a></li> 130<li><a href="https://nodejs.org/docs/latest-v8.x/api/path.html">8.x</a></li> 131<li><a href="https://nodejs.org/docs/latest-v7.x/api/path.html">7.x</a></li> 132<li><a href="https://nodejs.org/docs/latest-v6.x/api/path.html">6.x</a></li> 133<li><a href="https://nodejs.org/docs/latest-v5.x/api/path.html">5.x</a></li> 134<li><a href="https://nodejs.org/docs/latest-v4.x/api/path.html">4.x</a></li> 135<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/path.html">0.12.x</a></li> 136<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/path.html">0.10.x</a></li></ol> 137 </li> 138 139 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/path.md">Edit on GitHub</a></li> 140 </ul> 141 </div> 142 <hr> 143 </header> 144 145 <details id="toc" open><summary>Table of contents</summary><ul> 146<li><span class="stability_2"><a href="#path_path">Path</a></span> 147<ul> 148<li><a href="#path_windows_vs_posix">Windows vs. POSIX</a></li> 149<li><a href="#path_path_basename_path_ext"><code>path.basename(path[, ext])</code></a></li> 150<li><a href="#path_path_delimiter"><code>path.delimiter</code></a></li> 151<li><a href="#path_path_dirname_path"><code>path.dirname(path)</code></a></li> 152<li><a href="#path_path_extname_path"><code>path.extname(path)</code></a></li> 153<li><a href="#path_path_format_pathobject"><code>path.format(pathObject)</code></a></li> 154<li><a href="#path_path_isabsolute_path"><code>path.isAbsolute(path)</code></a></li> 155<li><a href="#path_path_join_paths"><code>path.join([...paths])</code></a></li> 156<li><a href="#path_path_normalize_path"><code>path.normalize(path)</code></a></li> 157<li><a href="#path_path_parse_path"><code>path.parse(path)</code></a></li> 158<li><a href="#path_path_posix"><code>path.posix</code></a></li> 159<li><a href="#path_path_relative_from_to"><code>path.relative(from, to)</code></a></li> 160<li><a href="#path_path_resolve_paths"><code>path.resolve([...paths])</code></a></li> 161<li><a href="#path_path_sep"><code>path.sep</code></a></li> 162<li><a href="#path_path_tonamespacedpath_path"><code>path.toNamespacedPath(path)</code></a></li> 163<li><a href="#path_path_win32"><code>path.win32</code></a></li> 164</ul> 165</li> 166</ul></details> 167 168 <div id="apicontent"> 169 <h2>Path<span><a class="mark" href="#path_path" id="path_path">#</a></span></h2> 170 171<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> 172<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.21.2/lib/path.js">lib/path.js</a></p> 173<p>The <code>path</code> module provides utilities for working with file and directory paths. 174It can be accessed using:</p> 175<pre><code class="language-js"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">'path'</span>);</code></pre> 176<section><h3>Windows vs. POSIX<span><a class="mark" href="#path_windows_vs_posix" id="path_windows_vs_posix">#</a></span></h3> 177<p>The default operation of the <code>path</code> module varies based on the operating system 178on which a Node.js application is running. Specifically, when running on a 179Windows operating system, the <code>path</code> module will assume that Windows-style 180paths are being used.</p> 181<p>So using <code>path.basename()</code> might yield different results on POSIX and Windows:</p> 182<p>On POSIX:</p> 183<pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'C:\\temp\\myfile.html'</span>); 184<span class="hljs-comment">// Returns: 'C:\\temp\\myfile.html'</span></code></pre> 185<p>On Windows:</p> 186<pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'C:\\temp\\myfile.html'</span>); 187<span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre> 188<p>To achieve consistent results when working with Windows file paths on any 189operating system, use <a href="#path_path_win32"><code>path.win32</code></a>:</p> 190<p>On POSIX and Windows:</p> 191<pre><code class="language-js">path.<span class="hljs-property">win32</span>.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'C:\\temp\\myfile.html'</span>); 192<span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre> 193<p>To achieve consistent results when working with POSIX file paths on any 194operating system, use <a href="#path_path_posix"><code>path.posix</code></a>:</p> 195<p>On POSIX and Windows:</p> 196<pre><code class="language-js">path.<span class="hljs-property">posix</span>.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'/tmp/myfile.html'</span>); 197<span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre> 198<p>On Windows Node.js follows the concept of per-drive working directory. 199This behavior can be observed when using a drive path without a backslash. For 200example, <code>path.resolve('C:\\')</code> can potentially return a different result than 201<code>path.resolve('C:')</code>. For more information, see 202<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths">this MSDN page</a>.</p> 203</section><section><h3><code>path.basename(path[, ext])</code><span><a class="mark" href="#path_path_basename_path_ext" id="path_path_basename_path_ext">#</a></span></h3> 204<div class="api_metadata"> 205<details class="changelog"><summary>History</summary> 206<table> 207<tbody><tr><th>Version</th><th>Changes</th></tr> 208<tr><td>v6.0.0</td> 209<td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr> 210<tr><td>v0.1.25</td> 211<td><p><span>Added in: v0.1.25</span></p></td></tr> 212</tbody></table> 213</details> 214</div> 215<ul> 216<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 217<li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> An optional file extension</li> 218<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 219</ul> 220<p>The <code>path.basename()</code> method returns the last portion of a <code>path</code>, similar to 221the Unix <code>basename</code> command. Trailing directory separators are ignored, see 222<a href="#path_path_sep"><code>path.sep</code></a>.</p> 223<pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'/foo/bar/baz/asdf/quux.html'</span>); 224<span class="hljs-comment">// Returns: 'quux.html'</span> 225 226path.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'/foo/bar/baz/asdf/quux.html'</span>, <span class="hljs-string">'.html'</span>); 227<span class="hljs-comment">// Returns: 'quux'</span></code></pre> 228<p>Although Windows usually treats file names, including file extensions, in a 229case-insensitive manner, this function does not. For example, <code>C:\\foo.html</code> and 230<code>C:\\foo.HTML</code> refer to the same file, but <code>basename</code> treats the extension as a 231case-sensitive string:</p> 232<pre><code class="language-js">path.<span class="hljs-property">win32</span>.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'C:\\foo.html'</span>, <span class="hljs-string">'.html'</span>); 233<span class="hljs-comment">// Returns: 'foo'</span> 234 235path.<span class="hljs-property">win32</span>.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'C:\\foo.HTML'</span>, <span class="hljs-string">'.html'</span>); 236<span class="hljs-comment">// Returns: 'foo.HTML'</span></code></pre> 237<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string or if <code>ext</code> is given 238and is not a string.</p> 239</section><section><h3><code>path.delimiter</code><span><a class="mark" href="#path_path_delimiter" id="path_path_delimiter">#</a></span></h3> 240<div class="api_metadata"> 241<span>Added in: v0.9.3</span> 242</div> 243<ul> 244<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 245</ul> 246<p>Provides the platform-specific path delimiter:</p> 247<ul> 248<li><code>;</code> for Windows</li> 249<li><code>:</code> for POSIX</li> 250</ul> 251<p>For example, on POSIX:</p> 252<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">PATH</span>); 253<span class="hljs-comment">// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'</span> 254 255process.<span class="hljs-property">env</span>.<span class="hljs-property">PATH</span>.<span class="hljs-title function_">split</span>(path.<span class="hljs-property">delimiter</span>); 256<span class="hljs-comment">// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']</span></code></pre> 257<p>On Windows:</p> 258<pre><code class="language-js"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(process.<span class="hljs-property">env</span>.<span class="hljs-property">PATH</span>); 259<span class="hljs-comment">// Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'</span> 260 261process.<span class="hljs-property">env</span>.<span class="hljs-property">PATH</span>.<span class="hljs-title function_">split</span>(path.<span class="hljs-property">delimiter</span>); 262<span class="hljs-comment">// Returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']</span></code></pre> 263</section><section><h3><code>path.dirname(path)</code><span><a class="mark" href="#path_path_dirname_path" id="path_path_dirname_path">#</a></span></h3> 264<div class="api_metadata"> 265<details class="changelog"><summary>History</summary> 266<table> 267<tbody><tr><th>Version</th><th>Changes</th></tr> 268<tr><td>v6.0.0</td> 269<td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr> 270<tr><td>v0.1.16</td> 271<td><p><span>Added in: v0.1.16</span></p></td></tr> 272</tbody></table> 273</details> 274</div> 275<ul> 276<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 277<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 278</ul> 279<p>The <code>path.dirname()</code> method returns the directory name of a <code>path</code>, similar to 280the Unix <code>dirname</code> command. Trailing directory separators are ignored, see 281<a href="#path_path_sep"><code>path.sep</code></a>.</p> 282<pre><code class="language-js">path.<span class="hljs-title function_">dirname</span>(<span class="hljs-string">'/foo/bar/baz/asdf/quux'</span>); 283<span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span></code></pre> 284<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 285</section><section><h3><code>path.extname(path)</code><span><a class="mark" href="#path_path_extname_path" id="path_path_extname_path">#</a></span></h3> 286<div class="api_metadata"> 287<details class="changelog"><summary>History</summary> 288<table> 289<tbody><tr><th>Version</th><th>Changes</th></tr> 290<tr><td>v6.0.0</td> 291<td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr> 292<tr><td>v0.1.25</td> 293<td><p><span>Added in: v0.1.25</span></p></td></tr> 294</tbody></table> 295</details> 296</div> 297<ul> 298<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 299<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 300</ul> 301<p>The <code>path.extname()</code> method returns the extension of the <code>path</code>, from the last 302occurrence of the <code>.</code> (period) character to end of string in the last portion of 303the <code>path</code>. If there is no <code>.</code> in the last portion of the <code>path</code>, or if 304there are no <code>.</code> characters other than the first character of 305the basename of <code>path</code> (see <code>path.basename()</code>) , an empty string is returned.</p> 306<pre><code class="language-js">path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.html'</span>); 307<span class="hljs-comment">// Returns: '.html'</span> 308 309path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.coffee.md'</span>); 310<span class="hljs-comment">// Returns: '.md'</span> 311 312path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.'</span>); 313<span class="hljs-comment">// Returns: '.'</span> 314 315path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index'</span>); 316<span class="hljs-comment">// Returns: ''</span> 317 318path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'.index'</span>); 319<span class="hljs-comment">// Returns: ''</span> 320 321path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'.index.md'</span>); 322<span class="hljs-comment">// Returns: '.md'</span></code></pre> 323<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 324</section><section><h3><code>path.format(pathObject)</code><span><a class="mark" href="#path_path_format_pathobject" id="path_path_format_pathobject">#</a></span></h3> 325<div class="api_metadata"> 326<span>Added in: v0.11.15</span> 327</div> 328<ul> 329<li><code>pathObject</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> Any JavaScript object having the following properties: 330<ul> 331<li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 332<li><code>root</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 333<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 334<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 335<li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 336</ul> 337</li> 338<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 339</ul> 340<p>The <code>path.format()</code> method returns a path string from an object. This is the 341opposite of <a href="#path_path_parse_path"><code>path.parse()</code></a>.</p> 342<p>When providing properties to the <code>pathObject</code> remember that there are 343combinations where one property has priority over another:</p> 344<ul> 345<li><code>pathObject.root</code> is ignored if <code>pathObject.dir</code> is provided</li> 346<li><code>pathObject.ext</code> and <code>pathObject.name</code> are ignored if <code>pathObject.base</code> exists</li> 347</ul> 348<p>For example, on POSIX:</p> 349<pre><code class="language-js"><span class="hljs-comment">// If `dir`, `root` and `base` are provided,</span> 350<span class="hljs-comment">// `${dir}${path.sep}${base}`</span> 351<span class="hljs-comment">// will be returned. `root` is ignored.</span> 352path.<span class="hljs-title function_">format</span>({ 353 <span class="hljs-attr">root</span>: <span class="hljs-string">'/ignored'</span>, 354 <span class="hljs-attr">dir</span>: <span class="hljs-string">'/home/user/dir'</span>, 355 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span> 356}); 357<span class="hljs-comment">// Returns: '/home/user/dir/file.txt'</span> 358 359<span class="hljs-comment">// `root` will be used if `dir` is not specified.</span> 360<span class="hljs-comment">// If only `root` is provided or `dir` is equal to `root` then the</span> 361<span class="hljs-comment">// platform separator will not be included. `ext` will be ignored.</span> 362path.<span class="hljs-title function_">format</span>({ 363 <span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>, 364 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>, 365 <span class="hljs-attr">ext</span>: <span class="hljs-string">'ignored'</span> 366}); 367<span class="hljs-comment">// Returns: '/file.txt'</span> 368 369<span class="hljs-comment">// `name` + `ext` will be used if `base` is not specified.</span> 370path.<span class="hljs-title function_">format</span>({ 371 <span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>, 372 <span class="hljs-attr">name</span>: <span class="hljs-string">'file'</span>, 373 <span class="hljs-attr">ext</span>: <span class="hljs-string">'.txt'</span> 374}); 375<span class="hljs-comment">// Returns: '/file.txt'</span></code></pre> 376<p>On Windows:</p> 377<pre><code class="language-js">path.<span class="hljs-title function_">format</span>({ 378 <span class="hljs-attr">dir</span>: <span class="hljs-string">'C:\\path\\dir'</span>, 379 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span> 380}); 381<span class="hljs-comment">// Returns: 'C:\\path\\dir\\file.txt'</span></code></pre> 382</section><section><h3><code>path.isAbsolute(path)</code><span><a class="mark" href="#path_path_isabsolute_path" id="path_path_isabsolute_path">#</a></span></h3> 383<div class="api_metadata"> 384<span>Added in: v0.11.2</span> 385</div> 386<ul> 387<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 388<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 389</ul> 390<p>The <code>path.isAbsolute()</code> method determines if <code>path</code> is an absolute path.</p> 391<p>If the given <code>path</code> is a zero-length string, <code>false</code> will be returned.</p> 392<p>For example, on POSIX:</p> 393<pre><code class="language-js">path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'/foo/bar'</span>); <span class="hljs-comment">// true</span> 394path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'/baz/..'</span>); <span class="hljs-comment">// true</span> 395path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'qux/'</span>); <span class="hljs-comment">// false</span> 396path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'.'</span>); <span class="hljs-comment">// false</span></code></pre> 397<p>On Windows:</p> 398<pre><code class="language-js">path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'//server'</span>); <span class="hljs-comment">// true</span> 399path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'\\\\server'</span>); <span class="hljs-comment">// true</span> 400path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'C:/foo/..'</span>); <span class="hljs-comment">// true</span> 401path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'C:\\foo\\..'</span>); <span class="hljs-comment">// true</span> 402path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'bar\\baz'</span>); <span class="hljs-comment">// false</span> 403path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'bar/baz'</span>); <span class="hljs-comment">// false</span> 404path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'.'</span>); <span class="hljs-comment">// false</span></code></pre> 405<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 406</section><section><h3><code>path.join([...paths])</code><span><a class="mark" href="#path_path_join_paths" id="path_path_join_paths">#</a></span></h3> 407<div class="api_metadata"> 408<span>Added in: v0.1.16</span> 409</div> 410<ul> 411<li><code>...paths</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A sequence of path segments</li> 412<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 413</ul> 414<p>The <code>path.join()</code> method joins all given <code>path</code> segments together using the 415platform-specific separator as a delimiter, then normalizes the resulting path.</p> 416<p>Zero-length <code>path</code> segments are ignored. If the joined path string is a 417zero-length string then <code>'.'</code> will be returned, representing the current 418working directory.</p> 419<pre><code class="language-js">path.<span class="hljs-title function_">join</span>(<span class="hljs-string">'/foo'</span>, <span class="hljs-string">'bar'</span>, <span class="hljs-string">'baz/asdf'</span>, <span class="hljs-string">'quux'</span>, <span class="hljs-string">'..'</span>); 420<span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span> 421 422path.<span class="hljs-title function_">join</span>(<span class="hljs-string">'foo'</span>, {}, <span class="hljs-string">'bar'</span>); 423<span class="hljs-comment">// Throws 'TypeError: Path must be a string. Received {}'</span></code></pre> 424<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if any of the path segments is not a string.</p> 425</section><section><h3><code>path.normalize(path)</code><span><a class="mark" href="#path_path_normalize_path" id="path_path_normalize_path">#</a></span></h3> 426<div class="api_metadata"> 427<span>Added in: v0.1.23</span> 428</div> 429<ul> 430<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 431<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 432</ul> 433<p>The <code>path.normalize()</code> method normalizes the given <code>path</code>, resolving <code>'..'</code> and 434<code>'.'</code> segments.</p> 435<p>When multiple, sequential path segment separation characters are found (e.g. 436<code>/</code> on POSIX and either <code>\</code> or <code>/</code> on Windows), they are replaced by a single 437instance of the platform-specific path segment separator (<code>/</code> on POSIX and 438<code>\</code> on Windows). Trailing separators are preserved.</p> 439<p>If the <code>path</code> is a zero-length string, <code>'.'</code> is returned, representing the 440current working directory.</p> 441<p>For example, on POSIX:</p> 442<pre><code class="language-js">path.<span class="hljs-title function_">normalize</span>(<span class="hljs-string">'/foo/bar//baz/asdf/quux/..'</span>); 443<span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span></code></pre> 444<p>On Windows:</p> 445<pre><code class="language-js">path.<span class="hljs-title function_">normalize</span>(<span class="hljs-string">'C:\\temp\\\\foo\\bar\\..\\'</span>); 446<span class="hljs-comment">// Returns: 'C:\\temp\\foo\\'</span></code></pre> 447<p>Since Windows recognizes multiple path separators, both separators will be 448replaced by instances of the Windows preferred separator (<code>\</code>):</p> 449<pre><code class="language-js">path.<span class="hljs-property">win32</span>.<span class="hljs-title function_">normalize</span>(<span class="hljs-string">'C:////temp\\\\/\\/\\/foo/bar'</span>); 450<span class="hljs-comment">// Returns: 'C:\\temp\\foo\\bar'</span></code></pre> 451<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 452</section><section><h3><code>path.parse(path)</code><span><a class="mark" href="#path_path_parse_path" id="path_path_parse_path">#</a></span></h3> 453<div class="api_metadata"> 454<span>Added in: v0.11.15</span> 455</div> 456<ul> 457<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 458<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 459</ul> 460<p>The <code>path.parse()</code> method returns an object whose properties represent 461significant elements of the <code>path</code>. Trailing directory separators are ignored, 462see <a href="#path_path_sep"><code>path.sep</code></a>.</p> 463<p>The returned object will have the following properties:</p> 464<ul> 465<li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 466<li><code>root</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 467<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 468<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 469<li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 470</ul> 471<p>For example, on POSIX:</p> 472<pre><code class="language-js">path.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'/home/user/dir/file.txt'</span>); 473<span class="hljs-comment">// Returns:</span> 474<span class="hljs-comment">// { root: '/',</span> 475<span class="hljs-comment">// dir: '/home/user/dir',</span> 476<span class="hljs-comment">// base: 'file.txt',</span> 477<span class="hljs-comment">// ext: '.txt',</span> 478<span class="hljs-comment">// name: 'file' }</span></code></pre> 479<pre><code class="language-text">┌─────────────────────┬────────────┐ 480│ dir │ base │ 481├──────┬ ├──────┬─────┤ 482│ root │ │ name │ ext │ 483" / home/user/dir / file .txt " 484└──────┴──────────────┴──────┴─────┘ 485(All spaces in the "" line should be ignored. They are purely for formatting.)</code></pre> 486<p>On Windows:</p> 487<pre><code class="language-js">path.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'C:\\path\\dir\\file.txt'</span>); 488<span class="hljs-comment">// Returns:</span> 489<span class="hljs-comment">// { root: 'C:\\',</span> 490<span class="hljs-comment">// dir: 'C:\\path\\dir',</span> 491<span class="hljs-comment">// base: 'file.txt',</span> 492<span class="hljs-comment">// ext: '.txt',</span> 493<span class="hljs-comment">// name: 'file' }</span></code></pre> 494<pre><code class="language-text">┌─────────────────────┬────────────┐ 495│ dir │ base │ 496├──────┬ ├──────┬─────┤ 497│ root │ │ name │ ext │ 498" C:\ path\dir \ file .txt " 499└──────┴──────────────┴──────┴─────┘ 500(All spaces in the "" line should be ignored. They are purely for formatting.)</code></pre> 501<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 502</section><section><h3><code>path.posix</code><span><a class="mark" href="#path_path_posix" id="path_path_posix">#</a></span></h3> 503<div class="api_metadata"> 504<span>Added in: v0.11.15</span> 505</div> 506<ul> 507<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 508</ul> 509<p>The <code>path.posix</code> property provides access to POSIX specific implementations 510of the <code>path</code> methods.</p> 511</section><section><h3><code>path.relative(from, to)</code><span><a class="mark" href="#path_path_relative_from_to" id="path_path_relative_from_to">#</a></span></h3> 512<div class="api_metadata"> 513<details class="changelog"><summary>History</summary> 514<table> 515<tbody><tr><th>Version</th><th>Changes</th></tr> 516<tr><td>v6.8.0</td> 517<td><p>On Windows, the leading slashes for UNC paths are now included in the return value.</p></td></tr> 518<tr><td>v0.5.0</td> 519<td><p><span>Added in: v0.5.0</span></p></td></tr> 520</tbody></table> 521</details> 522</div> 523<ul> 524<li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 525<li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 526<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 527</ul> 528<p>The <code>path.relative()</code> method returns the relative path from <code>from</code> to <code>to</code> based 529on the current working directory. If <code>from</code> and <code>to</code> each resolve to the same 530path (after calling <code>path.resolve()</code> on each), a zero-length string is returned.</p> 531<p>If a zero-length string is passed as <code>from</code> or <code>to</code>, the current working 532directory will be used instead of the zero-length strings.</p> 533<p>For example, on POSIX:</p> 534<pre><code class="language-js">path.<span class="hljs-title function_">relative</span>(<span class="hljs-string">'/data/orandea/test/aaa'</span>, <span class="hljs-string">'/data/orandea/impl/bbb'</span>); 535<span class="hljs-comment">// Returns: '../../impl/bbb'</span></code></pre> 536<p>On Windows:</p> 537<pre><code class="language-js">path.<span class="hljs-title function_">relative</span>(<span class="hljs-string">'C:\\orandea\\test\\aaa'</span>, <span class="hljs-string">'C:\\orandea\\impl\\bbb'</span>); 538<span class="hljs-comment">// Returns: '..\\..\\impl\\bbb'</span></code></pre> 539<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if either <code>from</code> or <code>to</code> is not a string.</p> 540</section><section><h3><code>path.resolve([...paths])</code><span><a class="mark" href="#path_path_resolve_paths" id="path_path_resolve_paths">#</a></span></h3> 541<div class="api_metadata"> 542<span>Added in: v0.3.4</span> 543</div> 544<ul> 545<li><code>...paths</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A sequence of paths or path segments</li> 546<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 547</ul> 548<p>The <code>path.resolve()</code> method resolves a sequence of paths or path segments into 549an absolute path.</p> 550<p>The given sequence of paths is processed from right to left, with each 551subsequent <code>path</code> prepended until an absolute path is constructed. 552For instance, given the sequence of path segments: <code>/foo</code>, <code>/bar</code>, <code>baz</code>, 553calling <code>path.resolve('/foo', '/bar', 'baz')</code> would return <code>/bar/baz</code> 554because <code>'baz'</code> is not an absolute path but <code>'/bar' + '/' + 'baz'</code> is.</p> 555<p>If, after processing all given <code>path</code> segments, an absolute path has not yet 556been generated, the current working directory is used.</p> 557<p>The resulting path is normalized and trailing slashes are removed unless the 558path is resolved to the root directory.</p> 559<p>Zero-length <code>path</code> segments are ignored.</p> 560<p>If no <code>path</code> segments are passed, <code>path.resolve()</code> will return the absolute path 561of the current working directory.</p> 562<pre><code class="language-js">path.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'/foo/bar'</span>, <span class="hljs-string">'./baz'</span>); 563<span class="hljs-comment">// Returns: '/foo/bar/baz'</span> 564 565path.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'/foo/bar'</span>, <span class="hljs-string">'/tmp/file/'</span>); 566<span class="hljs-comment">// Returns: '/tmp/file'</span> 567 568path.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'wwwroot'</span>, <span class="hljs-string">'static_files/png/'</span>, <span class="hljs-string">'../gif/image.gif'</span>); 569<span class="hljs-comment">// If the current working directory is /home/myself/node,</span> 570<span class="hljs-comment">// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'</span></code></pre> 571<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if any of the arguments is not a string.</p> 572</section><section><h3><code>path.sep</code><span><a class="mark" href="#path_path_sep" id="path_path_sep">#</a></span></h3> 573<div class="api_metadata"> 574<span>Added in: v0.7.9</span> 575</div> 576<ul> 577<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 578</ul> 579<p>Provides the platform-specific path segment separator:</p> 580<ul> 581<li><code>\</code> on Windows</li> 582<li><code>/</code> on POSIX</li> 583</ul> 584<p>For example, on POSIX:</p> 585<pre><code class="language-js"><span class="hljs-string">'foo/bar/baz'</span>.<span class="hljs-title function_">split</span>(path.<span class="hljs-property">sep</span>); 586<span class="hljs-comment">// Returns: ['foo', 'bar', 'baz']</span></code></pre> 587<p>On Windows:</p> 588<pre><code class="language-js"><span class="hljs-string">'foo\\bar\\baz'</span>.<span class="hljs-title function_">split</span>(path.<span class="hljs-property">sep</span>); 589<span class="hljs-comment">// Returns: ['foo', 'bar', 'baz']</span></code></pre> 590<p>On Windows, both the forward slash (<code>/</code>) and backward slash (<code>\</code>) are accepted 591as path segment separators; however, the <code>path</code> methods only add backward 592slashes (<code>\</code>).</p> 593</section><section><h3><code>path.toNamespacedPath(path)</code><span><a class="mark" href="#path_path_tonamespacedpath_path" id="path_path_tonamespacedpath_path">#</a></span></h3> 594<div class="api_metadata"> 595<span>Added in: v9.0.0</span> 596</div> 597<ul> 598<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 599<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 600</ul> 601<p>On Windows systems only, returns an equivalent <a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#namespaces">namespace-prefixed path</a> for 602the given <code>path</code>. If <code>path</code> is not a string, <code>path</code> will be returned without 603modifications.</p> 604<p>This method is meaningful only on Windows systems. On POSIX systems, the 605method is non-operational and always returns <code>path</code> without modifications.</p> 606</section><section><h3><code>path.win32</code><span><a class="mark" href="#path_path_win32" id="path_path_win32">#</a></span></h3> 607<div class="api_metadata"> 608<span>Added in: v0.11.15</span> 609</div> 610<ul> 611<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 612</ul> 613<p>The <code>path.win32</code> property provides access to Windows-specific implementations 614of the <code>path</code> methods.</p></section> 615 <!-- API END --> 616 </div> 617 </div> 618 </div> 619 <script> 620 'use strict'; 621 { 622 const kCustomPreference = 'customDarkTheme'; 623 const userSettings = sessionStorage.getItem(kCustomPreference); 624 const themeToggleButton = document.getElementById('theme-toggle-btn'); 625 if (userSettings === null && window.matchMedia) { 626 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 627 if ('onchange' in mq) { 628 function mqChangeListener(e) { 629 document.body.classList.toggle('dark-mode', e.matches); 630 } 631 mq.addEventListener('change', mqChangeListener); 632 if (themeToggleButton) { 633 themeToggleButton.addEventListener('click', function() { 634 mq.removeEventListener('change', mqChangeListener); 635 }, { once: true }); 636 } 637 } 638 if (mq.matches) { 639 document.body.classList.add('dark-mode'); 640 } 641 } else if (userSettings === 'true') { 642 document.body.classList.add('dark-mode'); 643 } 644 if (themeToggleButton) { 645 themeToggleButton.hidden = false; 646 themeToggleButton.addEventListener('click', function() { 647 sessionStorage.setItem( 648 kCustomPreference, 649 document.body.classList.toggle('dark-mode') 650 ); 651 }); 652 } 653 } 654 </script> 655</body> 656</html> 657