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