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.20.1"> 7 <title>Path | Node.js v14.20.1 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.20.1 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-v18.x/api/path.html">18.x</a></li> 120<li><a href="https://nodejs.org/docs/latest-v17.x/api/path.html">17.x</a></li> 121<li><a href="https://nodejs.org/docs/latest-v16.x/api/path.html">16.x <b>LTS</b></a></li> 122<li><a href="https://nodejs.org/docs/latest-v15.x/api/path.html">15.x</a></li> 123<li><a href="https://nodejs.org/docs/latest-v14.x/api/path.html">14.x <b>LTS</b></a></li> 124<li><a href="https://nodejs.org/docs/latest-v13.x/api/path.html">13.x</a></li> 125<li><a href="https://nodejs.org/docs/latest-v12.x/api/path.html">12.x <b>LTS</b></a></li> 126<li><a href="https://nodejs.org/docs/latest-v11.x/api/path.html">11.x</a></li> 127<li><a href="https://nodejs.org/docs/latest-v10.x/api/path.html">10.x</a></li> 128<li><a href="https://nodejs.org/docs/latest-v9.x/api/path.html">9.x</a></li> 129<li><a href="https://nodejs.org/docs/latest-v8.x/api/path.html">8.x</a></li> 130<li><a href="https://nodejs.org/docs/latest-v7.x/api/path.html">7.x</a></li> 131<li><a href="https://nodejs.org/docs/latest-v6.x/api/path.html">6.x</a></li> 132<li><a href="https://nodejs.org/docs/latest-v5.x/api/path.html">5.x</a></li> 133<li><a href="https://nodejs.org/docs/latest-v4.x/api/path.html">4.x</a></li> 134<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/path.html">0.12.x</a></li> 135<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/path.html">0.10.x</a></li></ol> 136 </li> 137 138 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/path.md">Edit on GitHub</a></li> 139 </ul> 140 </div> 141 <hr> 142 </header> 143 144 <details id="toc" open><summary>Table of contents</summary><ul> 145<li><span class="stability_2"><a href="#path_path">Path</a></span> 146<ul> 147<li><a href="#path_windows_vs_posix">Windows vs. POSIX</a></li> 148<li><a href="#path_path_basename_path_ext"><code>path.basename(path[, ext])</code></a></li> 149<li><a href="#path_path_delimiter"><code>path.delimiter</code></a></li> 150<li><a href="#path_path_dirname_path"><code>path.dirname(path)</code></a></li> 151<li><a href="#path_path_extname_path"><code>path.extname(path)</code></a></li> 152<li><a href="#path_path_format_pathobject"><code>path.format(pathObject)</code></a></li> 153<li><a href="#path_path_isabsolute_path"><code>path.isAbsolute(path)</code></a></li> 154<li><a href="#path_path_join_paths"><code>path.join([...paths])</code></a></li> 155<li><a href="#path_path_normalize_path"><code>path.normalize(path)</code></a></li> 156<li><a href="#path_path_parse_path"><code>path.parse(path)</code></a></li> 157<li><a href="#path_path_posix"><code>path.posix</code></a></li> 158<li><a href="#path_path_relative_from_to"><code>path.relative(from, to)</code></a></li> 159<li><a href="#path_path_resolve_paths"><code>path.resolve([...paths])</code></a></li> 160<li><a href="#path_path_sep"><code>path.sep</code></a></li> 161<li><a href="#path_path_tonamespacedpath_path"><code>path.toNamespacedPath(path)</code></a></li> 162<li><a href="#path_path_win32"><code>path.win32</code></a></li> 163</ul> 164</li> 165</ul></details> 166 167 <div id="apicontent"> 168 <h2>Path<span><a class="mark" href="#path_path" id="path_path">#</a></span></h2> 169 170<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p> 171<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v14.20.1/lib/path.js">lib/path.js</a></p> 172<p>The <code>path</code> module provides utilities for working with file and directory paths. 173It can be accessed using:</p> 174<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> 175<section><h3>Windows vs. POSIX<span><a class="mark" href="#path_windows_vs_posix" id="path_windows_vs_posix">#</a></span></h3> 176<p>The default operation of the <code>path</code> module varies based on the operating system 177on which a Node.js application is running. Specifically, when running on a 178Windows operating system, the <code>path</code> module will assume that Windows-style 179paths are being used.</p> 180<p>So using <code>path.basename()</code> might yield different results on POSIX and Windows:</p> 181<p>On POSIX:</p> 182<pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'C:\\temp\\myfile.html'</span>); 183<span class="hljs-comment">// Returns: 'C:\\temp\\myfile.html'</span></code></pre> 184<p>On Windows:</p> 185<pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'C:\\temp\\myfile.html'</span>); 186<span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre> 187<p>To achieve consistent results when working with Windows file paths on any 188operating system, use <a href="#path_path_win32"><code>path.win32</code></a>:</p> 189<p>On POSIX and Windows:</p> 190<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>); 191<span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre> 192<p>To achieve consistent results when working with POSIX file paths on any 193operating system, use <a href="#path_path_posix"><code>path.posix</code></a>:</p> 194<p>On POSIX and Windows:</p> 195<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>); 196<span class="hljs-comment">// Returns: 'myfile.html'</span></code></pre> 197<p>On Windows Node.js follows the concept of per-drive working directory. 198This behavior can be observed when using a drive path without a backslash. For 199example, <code>path.resolve('C:\\')</code> can potentially return a different result than 200<code>path.resolve('C:')</code>. For more information, see 201<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths">this MSDN page</a>.</p> 202</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> 203<div class="api_metadata"> 204<details class="changelog"><summary>History</summary> 205<table> 206<tbody><tr><th>Version</th><th>Changes</th></tr> 207<tr><td>v6.0.0</td> 208<td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr> 209<tr><td>v0.1.25</td> 210<td><p><span>Added in: v0.1.25</span></p></td></tr> 211</tbody></table> 212</details> 213</div> 214<ul> 215<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 216<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> 217<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 218</ul> 219<p>The <code>path.basename()</code> method returns the last portion of a <code>path</code>, similar to 220the Unix <code>basename</code> command. Trailing directory separators are ignored, see 221<a href="#path_path_sep"><code>path.sep</code></a>.</p> 222<pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'/foo/bar/baz/asdf/quux.html'</span>); 223<span class="hljs-comment">// Returns: 'quux.html'</span> 224 225path.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'/foo/bar/baz/asdf/quux.html'</span>, <span class="hljs-string">'.html'</span>); 226<span class="hljs-comment">// Returns: 'quux'</span></code></pre> 227<p>Although Windows usually treats file names, including file extensions, in a 228case-insensitive manner, this function does not. For example, <code>C:\\foo.html</code> and 229<code>C:\\foo.HTML</code> refer to the same file, but <code>basename</code> treats the extension as a 230case-sensitive string:</p> 231<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>); 232<span class="hljs-comment">// Returns: 'foo'</span> 233 234path.<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>); 235<span class="hljs-comment">// Returns: 'foo.HTML'</span></code></pre> 236<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 237and is not a string.</p> 238</section><section><h3><code>path.delimiter</code><span><a class="mark" href="#path_path_delimiter" id="path_path_delimiter">#</a></span></h3> 239<div class="api_metadata"> 240<span>Added in: v0.9.3</span> 241</div> 242<ul> 243<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 244</ul> 245<p>Provides the platform-specific path delimiter:</p> 246<ul> 247<li><code>;</code> for Windows</li> 248<li><code>:</code> for POSIX</li> 249</ul> 250<p>For example, on POSIX:</p> 251<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>); 252<span class="hljs-comment">// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'</span> 253 254process.<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>); 255<span class="hljs-comment">// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']</span></code></pre> 256<p>On Windows:</p> 257<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>); 258<span class="hljs-comment">// Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'</span> 259 260process.<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>); 261<span class="hljs-comment">// Returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']</span></code></pre> 262</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> 263<div class="api_metadata"> 264<details class="changelog"><summary>History</summary> 265<table> 266<tbody><tr><th>Version</th><th>Changes</th></tr> 267<tr><td>v6.0.0</td> 268<td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr> 269<tr><td>v0.1.16</td> 270<td><p><span>Added in: v0.1.16</span></p></td></tr> 271</tbody></table> 272</details> 273</div> 274<ul> 275<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 276<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 277</ul> 278<p>The <code>path.dirname()</code> method returns the directory name of a <code>path</code>, similar to 279the Unix <code>dirname</code> command. Trailing directory separators are ignored, see 280<a href="#path_path_sep"><code>path.sep</code></a>.</p> 281<pre><code class="language-js">path.<span class="hljs-title function_">dirname</span>(<span class="hljs-string">'/foo/bar/baz/asdf/quux'</span>); 282<span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span></code></pre> 283<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 284</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> 285<div class="api_metadata"> 286<details class="changelog"><summary>History</summary> 287<table> 288<tbody><tr><th>Version</th><th>Changes</th></tr> 289<tr><td>v6.0.0</td> 290<td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr> 291<tr><td>v0.1.25</td> 292<td><p><span>Added in: v0.1.25</span></p></td></tr> 293</tbody></table> 294</details> 295</div> 296<ul> 297<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 298<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 299</ul> 300<p>The <code>path.extname()</code> method returns the extension of the <code>path</code>, from the last 301occurrence of the <code>.</code> (period) character to end of string in the last portion of 302the <code>path</code>. If there is no <code>.</code> in the last portion of the <code>path</code>, or if 303there are no <code>.</code> characters other than the first character of 304the basename of <code>path</code> (see <code>path.basename()</code>) , an empty string is returned.</p> 305<pre><code class="language-js">path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.html'</span>); 306<span class="hljs-comment">// Returns: '.html'</span> 307 308path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.coffee.md'</span>); 309<span class="hljs-comment">// Returns: '.md'</span> 310 311path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.'</span>); 312<span class="hljs-comment">// Returns: '.'</span> 313 314path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index'</span>); 315<span class="hljs-comment">// Returns: ''</span> 316 317path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'.index'</span>); 318<span class="hljs-comment">// Returns: ''</span> 319 320path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'.index.md'</span>); 321<span class="hljs-comment">// Returns: '.md'</span></code></pre> 322<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 323</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> 324<div class="api_metadata"> 325<span>Added in: v0.11.15</span> 326</div> 327<ul> 328<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: 329<ul> 330<li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 331<li><code>root</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 332<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 333<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 334<li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 335</ul> 336</li> 337<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 338</ul> 339<p>The <code>path.format()</code> method returns a path string from an object. This is the 340opposite of <a href="#path_path_parse_path"><code>path.parse()</code></a>.</p> 341<p>When providing properties to the <code>pathObject</code> remember that there are 342combinations where one property has priority over another:</p> 343<ul> 344<li><code>pathObject.root</code> is ignored if <code>pathObject.dir</code> is provided</li> 345<li><code>pathObject.ext</code> and <code>pathObject.name</code> are ignored if <code>pathObject.base</code> exists</li> 346</ul> 347<p>For example, on POSIX:</p> 348<pre><code class="language-js"><span class="hljs-comment">// If `dir`, `root` and `base` are provided,</span> 349<span class="hljs-comment">// `${dir}${path.sep}${base}`</span> 350<span class="hljs-comment">// will be returned. `root` is ignored.</span> 351path.<span class="hljs-title function_">format</span>({ 352 <span class="hljs-attr">root</span>: <span class="hljs-string">'/ignored'</span>, 353 <span class="hljs-attr">dir</span>: <span class="hljs-string">'/home/user/dir'</span>, 354 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span> 355}); 356<span class="hljs-comment">// Returns: '/home/user/dir/file.txt'</span> 357 358<span class="hljs-comment">// `root` will be used if `dir` is not specified.</span> 359<span class="hljs-comment">// If only `root` is provided or `dir` is equal to `root` then the</span> 360<span class="hljs-comment">// platform separator will not be included. `ext` will be ignored.</span> 361path.<span class="hljs-title function_">format</span>({ 362 <span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>, 363 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>, 364 <span class="hljs-attr">ext</span>: <span class="hljs-string">'ignored'</span> 365}); 366<span class="hljs-comment">// Returns: '/file.txt'</span> 367 368<span class="hljs-comment">// `name` + `ext` will be used if `base` is not specified.</span> 369path.<span class="hljs-title function_">format</span>({ 370 <span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>, 371 <span class="hljs-attr">name</span>: <span class="hljs-string">'file'</span>, 372 <span class="hljs-attr">ext</span>: <span class="hljs-string">'.txt'</span> 373}); 374<span class="hljs-comment">// Returns: '/file.txt'</span></code></pre> 375<p>On Windows:</p> 376<pre><code class="language-js">path.<span class="hljs-title function_">format</span>({ 377 <span class="hljs-attr">dir</span>: <span class="hljs-string">'C:\\path\\dir'</span>, 378 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span> 379}); 380<span class="hljs-comment">// Returns: 'C:\\path\\dir\\file.txt'</span></code></pre> 381</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> 382<div class="api_metadata"> 383<span>Added in: v0.11.2</span> 384</div> 385<ul> 386<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 387<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 388</ul> 389<p>The <code>path.isAbsolute()</code> method determines if <code>path</code> is an absolute path.</p> 390<p>If the given <code>path</code> is a zero-length string, <code>false</code> will be returned.</p> 391<p>For example, on POSIX:</p> 392<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> 393path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'/baz/..'</span>); <span class="hljs-comment">// true</span> 394path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'qux/'</span>); <span class="hljs-comment">// false</span> 395path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'.'</span>); <span class="hljs-comment">// false</span></code></pre> 396<p>On Windows:</p> 397<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> 398path.<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">'C:/foo/..'</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">'bar\\baz'</span>); <span class="hljs-comment">// false</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">'.'</span>); <span class="hljs-comment">// false</span></code></pre> 404<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 405</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> 406<div class="api_metadata"> 407<span>Added in: v0.1.16</span> 408</div> 409<ul> 410<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> 411<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 412</ul> 413<p>The <code>path.join()</code> method joins all given <code>path</code> segments together using the 414platform-specific separator as a delimiter, then normalizes the resulting path.</p> 415<p>Zero-length <code>path</code> segments are ignored. If the joined path string is a 416zero-length string then <code>'.'</code> will be returned, representing the current 417working directory.</p> 418<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>); 419<span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span> 420 421path.<span class="hljs-title function_">join</span>(<span class="hljs-string">'foo'</span>, {}, <span class="hljs-string">'bar'</span>); 422<span class="hljs-comment">// Throws 'TypeError: Path must be a string. Received {}'</span></code></pre> 423<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> 424</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> 425<div class="api_metadata"> 426<span>Added in: v0.1.23</span> 427</div> 428<ul> 429<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 430<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 431</ul> 432<p>The <code>path.normalize()</code> method normalizes the given <code>path</code>, resolving <code>'..'</code> and 433<code>'.'</code> segments.</p> 434<p>When multiple, sequential path segment separation characters are found (e.g. 435<code>/</code> on POSIX and either <code>\</code> or <code>/</code> on Windows), they are replaced by a single 436instance of the platform-specific path segment separator (<code>/</code> on POSIX and 437<code>\</code> on Windows). Trailing separators are preserved.</p> 438<p>If the <code>path</code> is a zero-length string, <code>'.'</code> is returned, representing the 439current working directory.</p> 440<p>For example, on POSIX:</p> 441<pre><code class="language-js">path.<span class="hljs-title function_">normalize</span>(<span class="hljs-string">'/foo/bar//baz/asdf/quux/..'</span>); 442<span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span></code></pre> 443<p>On Windows:</p> 444<pre><code class="language-js">path.<span class="hljs-title function_">normalize</span>(<span class="hljs-string">'C:\\temp\\\\foo\\bar\\..\\'</span>); 445<span class="hljs-comment">// Returns: 'C:\\temp\\foo\\'</span></code></pre> 446<p>Since Windows recognizes multiple path separators, both separators will be 447replaced by instances of the Windows preferred separator (<code>\</code>):</p> 448<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>); 449<span class="hljs-comment">// Returns: 'C:\\temp\\foo\\bar'</span></code></pre> 450<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 451</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> 452<div class="api_metadata"> 453<span>Added in: v0.11.15</span> 454</div> 455<ul> 456<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 457<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 458</ul> 459<p>The <code>path.parse()</code> method returns an object whose properties represent 460significant elements of the <code>path</code>. Trailing directory separators are ignored, 461see <a href="#path_path_sep"><code>path.sep</code></a>.</p> 462<p>The returned object will have the following properties:</p> 463<ul> 464<li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 465<li><code>root</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 466<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 467<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 468<li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 469</ul> 470<p>For example, on POSIX:</p> 471<pre><code class="language-js">path.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'/home/user/dir/file.txt'</span>); 472<span class="hljs-comment">// Returns:</span> 473<span class="hljs-comment">// { root: '/',</span> 474<span class="hljs-comment">// dir: '/home/user/dir',</span> 475<span class="hljs-comment">// base: 'file.txt',</span> 476<span class="hljs-comment">// ext: '.txt',</span> 477<span class="hljs-comment">// name: 'file' }</span></code></pre> 478<pre><code class="language-text">┌─────────────────────┬────────────┐ 479│ dir │ base │ 480├──────┬ ├──────┬─────┤ 481│ root │ │ name │ ext │ 482" / home/user/dir / file .txt " 483└──────┴──────────────┴──────┴─────┘ 484(All spaces in the "" line should be ignored. They are purely for formatting.)</code></pre> 485<p>On Windows:</p> 486<pre><code class="language-js">path.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'C:\\path\\dir\\file.txt'</span>); 487<span class="hljs-comment">// Returns:</span> 488<span class="hljs-comment">// { root: 'C:\\',</span> 489<span class="hljs-comment">// dir: 'C:\\path\\dir',</span> 490<span class="hljs-comment">// base: 'file.txt',</span> 491<span class="hljs-comment">// ext: '.txt',</span> 492<span class="hljs-comment">// name: 'file' }</span></code></pre> 493<pre><code class="language-text">┌─────────────────────┬────────────┐ 494│ dir │ base │ 495├──────┬ ├──────┬─────┤ 496│ root │ │ name │ ext │ 497" C:\ path\dir \ file .txt " 498└──────┴──────────────┴──────┴─────┘ 499(All spaces in the "" line should be ignored. They are purely for formatting.)</code></pre> 500<p>A <a href="errors.html#errors_class_typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 501</section><section><h3><code>path.posix</code><span><a class="mark" href="#path_path_posix" id="path_path_posix">#</a></span></h3> 502<div class="api_metadata"> 503<span>Added in: v0.11.15</span> 504</div> 505<ul> 506<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 507</ul> 508<p>The <code>path.posix</code> property provides access to POSIX specific implementations 509of the <code>path</code> methods.</p> 510</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> 511<div class="api_metadata"> 512<details class="changelog"><summary>History</summary> 513<table> 514<tbody><tr><th>Version</th><th>Changes</th></tr> 515<tr><td>v6.8.0</td> 516<td><p>On Windows, the leading slashes for UNC paths are now included in the return value.</p></td></tr> 517<tr><td>v0.5.0</td> 518<td><p><span>Added in: v0.5.0</span></p></td></tr> 519</tbody></table> 520</details> 521</div> 522<ul> 523<li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 524<li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 525<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 526</ul> 527<p>The <code>path.relative()</code> method returns the relative path from <code>from</code> to <code>to</code> based 528on the current working directory. If <code>from</code> and <code>to</code> each resolve to the same 529path (after calling <code>path.resolve()</code> on each), a zero-length string is returned.</p> 530<p>If a zero-length string is passed as <code>from</code> or <code>to</code>, the current working 531directory will be used instead of the zero-length strings.</p> 532<p>For example, on POSIX:</p> 533<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>); 534<span class="hljs-comment">// Returns: '../../impl/bbb'</span></code></pre> 535<p>On Windows:</p> 536<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>); 537<span class="hljs-comment">// Returns: '..\\..\\impl\\bbb'</span></code></pre> 538<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> 539</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> 540<div class="api_metadata"> 541<span>Added in: v0.3.4</span> 542</div> 543<ul> 544<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> 545<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 546</ul> 547<p>The <code>path.resolve()</code> method resolves a sequence of paths or path segments into 548an absolute path.</p> 549<p>The given sequence of paths is processed from right to left, with each 550subsequent <code>path</code> prepended until an absolute path is constructed. 551For instance, given the sequence of path segments: <code>/foo</code>, <code>/bar</code>, <code>baz</code>, 552calling <code>path.resolve('/foo', '/bar', 'baz')</code> would return <code>/bar/baz</code> 553because <code>'baz'</code> is not an absolute path but <code>'/bar' + '/' + 'baz'</code> is.</p> 554<p>If, after processing all given <code>path</code> segments, an absolute path has not yet 555been generated, the current working directory is used.</p> 556<p>The resulting path is normalized and trailing slashes are removed unless the 557path is resolved to the root directory.</p> 558<p>Zero-length <code>path</code> segments are ignored.</p> 559<p>If no <code>path</code> segments are passed, <code>path.resolve()</code> will return the absolute path 560of the current working directory.</p> 561<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>); 562<span class="hljs-comment">// Returns: '/foo/bar/baz'</span> 563 564path.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'/foo/bar'</span>, <span class="hljs-string">'/tmp/file/'</span>); 565<span class="hljs-comment">// Returns: '/tmp/file'</span> 566 567path.<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>); 568<span class="hljs-comment">// If the current working directory is /home/myself/node,</span> 569<span class="hljs-comment">// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'</span></code></pre> 570<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> 571</section><section><h3><code>path.sep</code><span><a class="mark" href="#path_path_sep" id="path_path_sep">#</a></span></h3> 572<div class="api_metadata"> 573<span>Added in: v0.7.9</span> 574</div> 575<ul> 576<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 577</ul> 578<p>Provides the platform-specific path segment separator:</p> 579<ul> 580<li><code>\</code> on Windows</li> 581<li><code>/</code> on POSIX</li> 582</ul> 583<p>For example, on POSIX:</p> 584<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>); 585<span class="hljs-comment">// Returns: ['foo', 'bar', 'baz']</span></code></pre> 586<p>On Windows:</p> 587<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>); 588<span class="hljs-comment">// Returns: ['foo', 'bar', 'baz']</span></code></pre> 589<p>On Windows, both the forward slash (<code>/</code>) and backward slash (<code>\</code>) are accepted 590as path segment separators; however, the <code>path</code> methods only add backward 591slashes (<code>\</code>).</p> 592</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> 593<div class="api_metadata"> 594<span>Added in: v9.0.0</span> 595</div> 596<ul> 597<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 598<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 599</ul> 600<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 601the given <code>path</code>. If <code>path</code> is not a string, <code>path</code> will be returned without 602modifications.</p> 603<p>This method is meaningful only on Windows systems. On POSIX systems, the 604method is non-operational and always returns <code>path</code> without modifications.</p> 605</section><section><h3><code>path.win32</code><span><a class="mark" href="#path_path_win32" id="path_path_win32">#</a></span></h3> 606<div class="api_metadata"> 607<span>Added in: v0.11.15</span> 608</div> 609<ul> 610<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 611</ul> 612<p>The <code>path.win32</code> property provides access to Windows-specific implementations 613of the <code>path</code> methods.</p></section> 614 <!-- API END --> 615 </div> 616 </div> 617 </div> 618 <script> 619 'use strict'; 620 { 621 const kCustomPreference = 'customDarkTheme'; 622 const userSettings = sessionStorage.getItem(kCustomPreference); 623 const themeToggleButton = document.getElementById('theme-toggle-btn'); 624 if (userSettings === null && window.matchMedia) { 625 const mq = window.matchMedia('(prefers-color-scheme: dark)'); 626 if ('onchange' in mq) { 627 function mqChangeListener(e) { 628 document.body.classList.toggle('dark-mode', e.matches); 629 } 630 mq.addEventListener('change', mqChangeListener); 631 if (themeToggleButton) { 632 themeToggleButton.addEventListener('click', function() { 633 mq.removeEventListener('change', mqChangeListener); 634 }, { once: true }); 635 } 636 } 637 if (mq.matches) { 638 document.body.classList.add('dark-mode'); 639 } 640 } else if (userSettings === 'true') { 641 document.body.classList.add('dark-mode'); 642 } 643 if (themeToggleButton) { 644 themeToggleButton.hidden = false; 645 themeToggleButton.addEventListener('click', function() { 646 sessionStorage.setItem( 647 kCustomPreference, 648 document.body.classList.toggle('dark-mode') 649 ); 650 }); 651 } 652 } 653 </script> 654</body> 655</html> 656