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.20.1"> 7 <title>Path | Node.js v18.20.1 Documentation</title> 8 <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> 9 <link rel="stylesheet" href="assets/style.css"> 10 <link rel="stylesheet" href="assets/hljs.css"> 11 <link rel="canonical" href="https://nodejs.org/api/path.html"> 12 <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.20.1 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.20.1</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-v21.x/api/path.html">21.x</a></li> 238<li><a href="https://nodejs.org/docs/latest-v20.x/api/path.html">20.x <b>LTS</b></a></li> 239<li><a href="https://nodejs.org/docs/latest-v19.x/api/path.html">19.x</a></li> 240<li><a href="https://nodejs.org/docs/latest-v18.x/api/path.html">18.x <b>LTS</b></a></li> 241<li><a href="https://nodejs.org/docs/latest-v17.x/api/path.html">17.x</a></li> 242<li><a href="https://nodejs.org/docs/latest-v16.x/api/path.html">16.x</a></li> 243<li><a href="https://nodejs.org/docs/latest-v15.x/api/path.html">15.x</a></li> 244<li><a href="https://nodejs.org/docs/latest-v14.x/api/path.html">14.x</a></li> 245<li><a href="https://nodejs.org/docs/latest-v13.x/api/path.html">13.x</a></li> 246<li><a href="https://nodejs.org/docs/latest-v12.x/api/path.html">12.x</a></li> 247<li><a href="https://nodejs.org/docs/latest-v11.x/api/path.html">11.x</a></li> 248<li><a href="https://nodejs.org/docs/latest-v10.x/api/path.html">10.x</a></li> 249<li><a href="https://nodejs.org/docs/latest-v9.x/api/path.html">9.x</a></li> 250<li><a href="https://nodejs.org/docs/latest-v8.x/api/path.html">8.x</a></li> 251<li><a href="https://nodejs.org/docs/latest-v7.x/api/path.html">7.x</a></li> 252<li><a href="https://nodejs.org/docs/latest-v6.x/api/path.html">6.x</a></li> 253<li><a href="https://nodejs.org/docs/latest-v5.x/api/path.html">5.x</a></li> 254<li><a href="https://nodejs.org/docs/latest-v4.x/api/path.html">4.x</a></li> 255<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/path.html">0.12.x</a></li> 256<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/path.html">0.10.x</a></li></ol></div> 257 </li> 258 259 <li class="picker-header"> 260 <a href="#"> 261 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 262 Options 263 </a> 264 265 <div class="picker"> 266 <ul> 267 <li> 268 <a href="all.html">View on single page</a> 269 </li> 270 <li> 271 <a href="path.json">View as JSON</a> 272 </li> 273 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/path.md">Edit on GitHub</a></li> 274 </ul> 275 </div> 276 </li> 277 </ul> 278 </div> 279 <hr> 280 </header> 281 282 <details id="toc" open><summary>Table of contents</summary><ul> 283<li><span class="stability_2"><a href="#path">Path</a></span> 284<ul> 285<li><a href="#windows-vs-posix">Windows vs. POSIX</a></li> 286<li><a href="#pathbasenamepath-suffix"><code>path.basename(path[, suffix])</code></a></li> 287<li><a href="#pathdelimiter"><code>path.delimiter</code></a></li> 288<li><a href="#pathdirnamepath"><code>path.dirname(path)</code></a></li> 289<li><a href="#pathextnamepath"><code>path.extname(path)</code></a></li> 290<li><a href="#pathformatpathobject"><code>path.format(pathObject)</code></a></li> 291<li><a href="#pathisabsolutepath"><code>path.isAbsolute(path)</code></a></li> 292<li><a href="#pathjoinpaths"><code>path.join([...paths])</code></a></li> 293<li><a href="#pathnormalizepath"><code>path.normalize(path)</code></a></li> 294<li><a href="#pathparsepath"><code>path.parse(path)</code></a></li> 295<li><a href="#pathposix"><code>path.posix</code></a></li> 296<li><a href="#pathrelativefrom-to"><code>path.relative(from, to)</code></a></li> 297<li><a href="#pathresolvepaths"><code>path.resolve([...paths])</code></a></li> 298<li><a href="#pathsep"><code>path.sep</code></a></li> 299<li><a href="#pathtonamespacedpathpath"><code>path.toNamespacedPath(path)</code></a></li> 300<li><a href="#pathwin32"><code>path.win32</code></a></li> 301</ul> 302</li> 303</ul></details> 304 305 <div id="apicontent"> 306 <h2>Path<span><a class="mark" href="#path" id="path">#</a></span><a aria-hidden="true" class="legacy" id="path_path"></a></h2> 307 308<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 309<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/path.js">lib/path.js</a></p> 310<p>The <code>node:path</code> module provides utilities for working with file and directory 311paths. It can be accessed using:</p> 312<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> 313<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> 314<p>The default operation of the <code>node:path</code> module varies based on the operating 315system on which a Node.js application is running. Specifically, when running on 316a Windows operating system, the <code>node:path</code> module will assume that 317Windows-style paths are being used.</p> 318<p>So using <code>path.basename()</code> might yield different results on POSIX and Windows:</p> 319<p>On POSIX:</p> 320<pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'C:\\temp\\myfile.html'</span>); 321<span class="hljs-comment">// Returns: 'C:\\temp\\myfile.html'</span></code> <button class="copy-button">copy</button></pre> 322<p>On Windows:</p> 323<pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'C:\\temp\\myfile.html'</span>); 324<span class="hljs-comment">// Returns: 'myfile.html'</span></code> <button class="copy-button">copy</button></pre> 325<p>To achieve consistent results when working with Windows file paths on any 326operating system, use <a href="#pathwin32"><code>path.win32</code></a>:</p> 327<p>On POSIX and Windows:</p> 328<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>); 329<span class="hljs-comment">// Returns: 'myfile.html'</span></code> <button class="copy-button">copy</button></pre> 330<p>To achieve consistent results when working with POSIX file paths on any 331operating system, use <a href="#pathposix"><code>path.posix</code></a>:</p> 332<p>On POSIX and Windows:</p> 333<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>); 334<span class="hljs-comment">// Returns: 'myfile.html'</span></code> <button class="copy-button">copy</button></pre> 335<p>On Windows Node.js follows the concept of per-drive working directory. 336This behavior can be observed when using a drive path without a backslash. For 337example, <code>path.resolve('C:\\')</code> can potentially return a different result than 338<code>path.resolve('C:')</code>. For more information, see 339<a href="https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#fully-qualified-vs-relative-paths">this MSDN page</a>.</p> 340</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> 341<div class="api_metadata"> 342<details class="changelog"><summary>History</summary> 343<table> 344<tbody><tr><th>Version</th><th>Changes</th></tr> 345<tr><td>v6.0.0</td> 346<td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr> 347<tr><td>v0.1.25</td> 348<td><p><span>Added in: v0.1.25</span></p></td></tr> 349</tbody></table> 350</details> 351</div> 352<ul> 353<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 354<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> 355<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 356</ul> 357<p>The <code>path.basename()</code> method returns the last portion of a <code>path</code>, similar to 358the Unix <code>basename</code> command. Trailing <a href="#pathsep">directory separators</a> are 359ignored.</p> 360<pre><code class="language-js">path.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'/foo/bar/baz/asdf/quux.html'</span>); 361<span class="hljs-comment">// Returns: 'quux.html'</span> 362 363path.<span class="hljs-title function_">basename</span>(<span class="hljs-string">'/foo/bar/baz/asdf/quux.html'</span>, <span class="hljs-string">'.html'</span>); 364<span class="hljs-comment">// Returns: 'quux'</span></code> <button class="copy-button">copy</button></pre> 365<p>Although Windows usually treats file names, including file extensions, in a 366case-insensitive manner, this function does not. For example, <code>C:\\foo.html</code> and 367<code>C:\\foo.HTML</code> refer to the same file, but <code>basename</code> treats the extension as a 368case-sensitive string:</p> 369<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>); 370<span class="hljs-comment">// Returns: 'foo'</span> 371 372path.<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>); 373<span class="hljs-comment">// Returns: 'foo.HTML'</span></code> <button class="copy-button">copy</button></pre> 374<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 375and is not a string.</p> 376</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> 377<div class="api_metadata"> 378<span>Added in: v0.9.3</span> 379</div> 380<ul> 381<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 382</ul> 383<p>Provides the platform-specific path delimiter:</p> 384<ul> 385<li><code>;</code> for Windows</li> 386<li><code>:</code> for POSIX</li> 387</ul> 388<p>For example, on POSIX:</p> 389<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>); 390<span class="hljs-comment">// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'</span> 391 392process.<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>); 393<span class="hljs-comment">// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']</span></code> <button class="copy-button">copy</button></pre> 394<p>On Windows:</p> 395<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>); 396<span class="hljs-comment">// Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'</span> 397 398process.<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>); 399<span class="hljs-comment">// Returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']</span></code> <button class="copy-button">copy</button></pre> 400</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> 401<div class="api_metadata"> 402<details class="changelog"><summary>History</summary> 403<table> 404<tbody><tr><th>Version</th><th>Changes</th></tr> 405<tr><td>v6.0.0</td> 406<td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr> 407<tr><td>v0.1.16</td> 408<td><p><span>Added in: v0.1.16</span></p></td></tr> 409</tbody></table> 410</details> 411</div> 412<ul> 413<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 414<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 415</ul> 416<p>The <code>path.dirname()</code> method returns the directory name of a <code>path</code>, similar to 417the Unix <code>dirname</code> command. Trailing directory separators are ignored, see 418<a href="#pathsep"><code>path.sep</code></a>.</p> 419<pre><code class="language-js">path.<span class="hljs-title function_">dirname</span>(<span class="hljs-string">'/foo/bar/baz/asdf/quux'</span>); 420<span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span></code> <button class="copy-button">copy</button></pre> 421<p>A <a href="errors.html#class-typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 422</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> 423<div class="api_metadata"> 424<details class="changelog"><summary>History</summary> 425<table> 426<tbody><tr><th>Version</th><th>Changes</th></tr> 427<tr><td>v6.0.0</td> 428<td><p>Passing a non-string as the <code>path</code> argument will throw now.</p></td></tr> 429<tr><td>v0.1.25</td> 430<td><p><span>Added in: v0.1.25</span></p></td></tr> 431</tbody></table> 432</details> 433</div> 434<ul> 435<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 436<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 437</ul> 438<p>The <code>path.extname()</code> method returns the extension of the <code>path</code>, from the last 439occurrence of the <code>.</code> (period) character to end of string in the last portion of 440the <code>path</code>. If there is no <code>.</code> in the last portion of the <code>path</code>, or if 441there are no <code>.</code> characters other than the first character of 442the basename of <code>path</code> (see <code>path.basename()</code>) , an empty string is returned.</p> 443<pre><code class="language-js">path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.html'</span>); 444<span class="hljs-comment">// Returns: '.html'</span> 445 446path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.coffee.md'</span>); 447<span class="hljs-comment">// Returns: '.md'</span> 448 449path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index.'</span>); 450<span class="hljs-comment">// Returns: '.'</span> 451 452path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'index'</span>); 453<span class="hljs-comment">// Returns: ''</span> 454 455path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'.index'</span>); 456<span class="hljs-comment">// Returns: ''</span> 457 458path.<span class="hljs-title function_">extname</span>(<span class="hljs-string">'.index.md'</span>); 459<span class="hljs-comment">// Returns: '.md'</span></code> <button class="copy-button">copy</button></pre> 460<p>A <a href="errors.html#class-typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 461</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> 462<div class="api_metadata"> 463<span>Added in: v0.11.15</span> 464</div> 465<ul> 466<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: 467<ul> 468<li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 469<li><code>root</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 470<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 471<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 472<li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 473</ul> 474</li> 475<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 476</ul> 477<p>The <code>path.format()</code> method returns a path string from an object. This is the 478opposite of <a href="#pathparsepath"><code>path.parse()</code></a>.</p> 479<p>When providing properties to the <code>pathObject</code> remember that there are 480combinations where one property has priority over another:</p> 481<ul> 482<li><code>pathObject.root</code> is ignored if <code>pathObject.dir</code> is provided</li> 483<li><code>pathObject.ext</code> and <code>pathObject.name</code> are ignored if <code>pathObject.base</code> exists</li> 484</ul> 485<p>For example, on POSIX:</p> 486<pre><code class="language-js"><span class="hljs-comment">// If `dir`, `root` and `base` are provided,</span> 487<span class="hljs-comment">// `${dir}${path.sep}${base}`</span> 488<span class="hljs-comment">// will be returned. `root` is ignored.</span> 489path.<span class="hljs-title function_">format</span>({ 490 <span class="hljs-attr">root</span>: <span class="hljs-string">'/ignored'</span>, 491 <span class="hljs-attr">dir</span>: <span class="hljs-string">'/home/user/dir'</span>, 492 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>, 493}); 494<span class="hljs-comment">// Returns: '/home/user/dir/file.txt'</span> 495 496<span class="hljs-comment">// `root` will be used if `dir` is not specified.</span> 497<span class="hljs-comment">// If only `root` is provided or `dir` is equal to `root` then the</span> 498<span class="hljs-comment">// platform separator will not be included. `ext` will be ignored.</span> 499path.<span class="hljs-title function_">format</span>({ 500 <span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>, 501 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>, 502 <span class="hljs-attr">ext</span>: <span class="hljs-string">'ignored'</span>, 503}); 504<span class="hljs-comment">// Returns: '/file.txt'</span> 505 506<span class="hljs-comment">// `name` + `ext` will be used if `base` is not specified.</span> 507path.<span class="hljs-title function_">format</span>({ 508 <span class="hljs-attr">root</span>: <span class="hljs-string">'/'</span>, 509 <span class="hljs-attr">name</span>: <span class="hljs-string">'file'</span>, 510 <span class="hljs-attr">ext</span>: <span class="hljs-string">'.txt'</span>, 511}); 512<span class="hljs-comment">// Returns: '/file.txt'</span></code> <button class="copy-button">copy</button></pre> 513<p>On Windows:</p> 514<pre><code class="language-js">path.<span class="hljs-title function_">format</span>({ 515 <span class="hljs-attr">dir</span>: <span class="hljs-string">'C:\\path\\dir'</span>, 516 <span class="hljs-attr">base</span>: <span class="hljs-string">'file.txt'</span>, 517}); 518<span class="hljs-comment">// Returns: 'C:\\path\\dir\\file.txt'</span></code> <button class="copy-button">copy</button></pre> 519</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> 520<div class="api_metadata"> 521<span>Added in: v0.11.2</span> 522</div> 523<ul> 524<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 525<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> 526</ul> 527<p>The <code>path.isAbsolute()</code> method determines if <code>path</code> is an absolute path.</p> 528<p>If the given <code>path</code> is a zero-length string, <code>false</code> will be returned.</p> 529<p>For example, on POSIX:</p> 530<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> 531path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'/baz/..'</span>); <span class="hljs-comment">// true</span> 532path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'qux/'</span>); <span class="hljs-comment">// false</span> 533path.<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> 534<p>On Windows:</p> 535<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> 536path.<span class="hljs-title function_">isAbsolute</span>(<span class="hljs-string">'\\\\server'</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">'C:\\foo\\..'</span>); <span class="hljs-comment">// true</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">'bar/baz'</span>); <span class="hljs-comment">// false</span> 541path.<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> 542<p>A <a href="errors.html#class-typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 543</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> 544<div class="api_metadata"> 545<span>Added in: v0.1.16</span> 546</div> 547<ul> 548<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> 549<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 550</ul> 551<p>The <code>path.join()</code> method joins all given <code>path</code> segments together using the 552platform-specific separator as a delimiter, then normalizes the resulting path.</p> 553<p>Zero-length <code>path</code> segments are ignored. If the joined path string is a 554zero-length string then <code>'.'</code> will be returned, representing the current 555working directory.</p> 556<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>); 557<span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span> 558 559path.<span class="hljs-title function_">join</span>(<span class="hljs-string">'foo'</span>, {}, <span class="hljs-string">'bar'</span>); 560<span class="hljs-comment">// Throws 'TypeError: Path must be a string. Received {}'</span></code> <button class="copy-button">copy</button></pre> 561<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> 562</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> 563<div class="api_metadata"> 564<span>Added in: v0.1.23</span> 565</div> 566<ul> 567<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 568<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 569</ul> 570<p>The <code>path.normalize()</code> method normalizes the given <code>path</code>, resolving <code>'..'</code> and 571<code>'.'</code> segments.</p> 572<p>When multiple, sequential path segment separation characters are found (e.g. 573<code>/</code> on POSIX and either <code>\</code> or <code>/</code> on Windows), they are replaced by a single 574instance of the platform-specific path segment separator (<code>/</code> on POSIX and 575<code>\</code> on Windows). Trailing separators are preserved.</p> 576<p>If the <code>path</code> is a zero-length string, <code>'.'</code> is returned, representing the 577current working directory.</p> 578<p>For example, on POSIX:</p> 579<pre><code class="language-js">path.<span class="hljs-title function_">normalize</span>(<span class="hljs-string">'/foo/bar//baz/asdf/quux/..'</span>); 580<span class="hljs-comment">// Returns: '/foo/bar/baz/asdf'</span></code> <button class="copy-button">copy</button></pre> 581<p>On Windows:</p> 582<pre><code class="language-js">path.<span class="hljs-title function_">normalize</span>(<span class="hljs-string">'C:\\temp\\\\foo\\bar\\..\\'</span>); 583<span class="hljs-comment">// Returns: 'C:\\temp\\foo\\'</span></code> <button class="copy-button">copy</button></pre> 584<p>Since Windows recognizes multiple path separators, both separators will be 585replaced by instances of the Windows preferred separator (<code>\</code>):</p> 586<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>); 587<span class="hljs-comment">// Returns: 'C:\\temp\\foo\\bar'</span></code> <button class="copy-button">copy</button></pre> 588<p>A <a href="errors.html#class-typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 589</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> 590<div class="api_metadata"> 591<span>Added in: v0.11.15</span> 592</div> 593<ul> 594<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 595<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 596</ul> 597<p>The <code>path.parse()</code> method returns an object whose properties represent 598significant elements of the <code>path</code>. Trailing directory separators are ignored, 599see <a href="#pathsep"><code>path.sep</code></a>.</p> 600<p>The returned object will have the following properties:</p> 601<ul> 602<li><code>dir</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 603<li><code>root</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 604<li><code>base</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 605<li><code>name</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 606<li><code>ext</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 607</ul> 608<p>For example, on POSIX:</p> 609<pre><code class="language-js">path.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'/home/user/dir/file.txt'</span>); 610<span class="hljs-comment">// Returns:</span> 611<span class="hljs-comment">// { root: '/',</span> 612<span class="hljs-comment">// dir: '/home/user/dir',</span> 613<span class="hljs-comment">// base: 'file.txt',</span> 614<span class="hljs-comment">// ext: '.txt',</span> 615<span class="hljs-comment">// name: 'file' }</span></code> <button class="copy-button">copy</button></pre> 616<pre><code class="language-text">┌─────────────────────┬────────────┐ 617│ dir │ base │ 618├──────┬ ├──────┬─────┤ 619│ root │ │ name │ ext │ 620" / home/user/dir / file .txt " 621└──────┴──────────────┴──────┴─────┘ 622(All spaces in the "" line should be ignored. They are purely for formatting.)</code> <button class="copy-button">copy</button></pre> 623<p>On Windows:</p> 624<pre><code class="language-js">path.<span class="hljs-title function_">parse</span>(<span class="hljs-string">'C:\\path\\dir\\file.txt'</span>); 625<span class="hljs-comment">// Returns:</span> 626<span class="hljs-comment">// { root: 'C:\\',</span> 627<span class="hljs-comment">// dir: 'C:\\path\\dir',</span> 628<span class="hljs-comment">// base: 'file.txt',</span> 629<span class="hljs-comment">// ext: '.txt',</span> 630<span class="hljs-comment">// name: 'file' }</span></code> <button class="copy-button">copy</button></pre> 631<pre><code class="language-text">┌─────────────────────┬────────────┐ 632│ dir │ base │ 633├──────┬ ├──────┬─────┤ 634│ root │ │ name │ ext │ 635" C:\ path\dir \ file .txt " 636└──────┴──────────────┴──────┴─────┘ 637(All spaces in the "" line should be ignored. They are purely for formatting.)</code> <button class="copy-button">copy</button></pre> 638<p>A <a href="errors.html#class-typeerror"><code>TypeError</code></a> is thrown if <code>path</code> is not a string.</p> 639</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> 640<div class="api_metadata"> 641<details class="changelog"><summary>History</summary> 642<table> 643<tbody><tr><th>Version</th><th>Changes</th></tr> 644<tr><td>v15.3.0</td> 645<td><p>Exposed as <code>require('path/posix')</code>.</p></td></tr> 646<tr><td>v0.11.15</td> 647<td><p><span>Added in: v0.11.15</span></p></td></tr> 648</tbody></table> 649</details> 650</div> 651<ul> 652<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 653</ul> 654<p>The <code>path.posix</code> property provides access to POSIX specific implementations 655of the <code>path</code> methods.</p> 656<p>The API is accessible via <code>require('node:path').posix</code> or <code>require('node:path/posix')</code>.</p> 657</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> 658<div class="api_metadata"> 659<details class="changelog"><summary>History</summary> 660<table> 661<tbody><tr><th>Version</th><th>Changes</th></tr> 662<tr><td>v6.8.0</td> 663<td><p>On Windows, the leading slashes for UNC paths are now included in the return value.</p></td></tr> 664<tr><td>v0.5.0</td> 665<td><p><span>Added in: v0.5.0</span></p></td></tr> 666</tbody></table> 667</details> 668</div> 669<ul> 670<li><code>from</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 671<li><code>to</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 672<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 673</ul> 674<p>The <code>path.relative()</code> method returns the relative path from <code>from</code> to <code>to</code> based 675on the current working directory. If <code>from</code> and <code>to</code> each resolve to the same 676path (after calling <code>path.resolve()</code> on each), a zero-length string is returned.</p> 677<p>If a zero-length string is passed as <code>from</code> or <code>to</code>, the current working 678directory will be used instead of the zero-length strings.</p> 679<p>For example, on POSIX:</p> 680<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>); 681<span class="hljs-comment">// Returns: '../../impl/bbb'</span></code> <button class="copy-button">copy</button></pre> 682<p>On Windows:</p> 683<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>); 684<span class="hljs-comment">// Returns: '..\\..\\impl\\bbb'</span></code> <button class="copy-button">copy</button></pre> 685<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> 686</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> 687<div class="api_metadata"> 688<span>Added in: v0.3.4</span> 689</div> 690<ul> 691<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> 692<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 693</ul> 694<p>The <code>path.resolve()</code> method resolves a sequence of paths or path segments into 695an absolute path.</p> 696<p>The given sequence of paths is processed from right to left, with each 697subsequent <code>path</code> prepended until an absolute path is constructed. 698For instance, given the sequence of path segments: <code>/foo</code>, <code>/bar</code>, <code>baz</code>, 699calling <code>path.resolve('/foo', '/bar', 'baz')</code> would return <code>/bar/baz</code> 700because <code>'baz'</code> is not an absolute path but <code>'/bar' + '/' + 'baz'</code> is.</p> 701<p>If, after processing all given <code>path</code> segments, an absolute path has not yet 702been generated, the current working directory is used.</p> 703<p>The resulting path is normalized and trailing slashes are removed unless the 704path is resolved to the root directory.</p> 705<p>Zero-length <code>path</code> segments are ignored.</p> 706<p>If no <code>path</code> segments are passed, <code>path.resolve()</code> will return the absolute path 707of the current working directory.</p> 708<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>); 709<span class="hljs-comment">// Returns: '/foo/bar/baz'</span> 710 711path.<span class="hljs-title function_">resolve</span>(<span class="hljs-string">'/foo/bar'</span>, <span class="hljs-string">'/tmp/file/'</span>); 712<span class="hljs-comment">// Returns: '/tmp/file'</span> 713 714path.<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>); 715<span class="hljs-comment">// If the current working directory is /home/myself/node,</span> 716<span class="hljs-comment">// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'</span></code> <button class="copy-button">copy</button></pre> 717<p>A <a href="errors.html#class-typeerror"><code>TypeError</code></a> is thrown if any of the arguments is not a string.</p> 718</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> 719<div class="api_metadata"> 720<span>Added in: v0.7.9</span> 721</div> 722<ul> 723<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 724</ul> 725<p>Provides the platform-specific path segment separator:</p> 726<ul> 727<li><code>\</code> on Windows</li> 728<li><code>/</code> on POSIX</li> 729</ul> 730<p>For example, on POSIX:</p> 731<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>); 732<span class="hljs-comment">// Returns: ['foo', 'bar', 'baz']</span></code> <button class="copy-button">copy</button></pre> 733<p>On Windows:</p> 734<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>); 735<span class="hljs-comment">// Returns: ['foo', 'bar', 'baz']</span></code> <button class="copy-button">copy</button></pre> 736<p>On Windows, both the forward slash (<code>/</code>) and backward slash (<code>\</code>) are accepted 737as path segment separators; however, the <code>path</code> methods only add backward 738slashes (<code>\</code>).</p> 739</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> 740<div class="api_metadata"> 741<span>Added in: v9.0.0</span> 742</div> 743<ul> 744<li><code>path</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 745<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 746</ul> 747<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 748the given <code>path</code>. If <code>path</code> is not a string, <code>path</code> will be returned without 749modifications.</p> 750<p>This method is meaningful only on Windows systems. On POSIX systems, the 751method is non-operational and always returns <code>path</code> without modifications.</p> 752</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> 753<div class="api_metadata"> 754<details class="changelog"><summary>History</summary> 755<table> 756<tbody><tr><th>Version</th><th>Changes</th></tr> 757<tr><td>v15.3.0</td> 758<td><p>Exposed as <code>require('path/win32')</code>.</p></td></tr> 759<tr><td>v0.11.15</td> 760<td><p><span>Added in: v0.11.15</span></p></td></tr> 761</tbody></table> 762</details> 763</div> 764<ul> 765<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 766</ul> 767<p>The <code>path.win32</code> property provides access to Windows-specific implementations 768of the <code>path</code> methods.</p> 769<p>The API is accessible via <code>require('node:path').win32</code> or <code>require('node:path/win32')</code>.</p></section> 770 <!-- API END --> 771 </div> 772 </div> 773 </div> 774</body> 775</html> 776