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