| <!doctype html> |
| <html lang="en"> |
| <head> |
| <meta charset="utf-8"> |
| <title>Path Node.js v0.10.24 Manual & Documentation</title> |
| <link rel="stylesheet" href="assets/style.css"> |
| <link rel="stylesheet" href="assets/sh.css"> |
| <link rel="canonical" href="http://nodejs.org/api/path.html"> |
| </head> |
| <body class="alt apidoc" id="api-section-path"> |
| <div id="intro" class="interior"> |
| <a href="/" title="Go back to the home page"> |
| <img id="logo" src="http://nodejs.org/images/logo-light.png" alt="node.js"> |
| </a> |
| </div> |
| <div id="content" class="clearfix"> |
| <div id="column2" class="interior"> |
| <ul> |
| <li><a href="/" class="home">Home</a></li> |
| <li><a href="/download/" class="download">Download</a></li> |
| <li><a href="/about/" class="about">About</a></li> |
| <li><a href="http://npmjs.org/" class="npm">npm Registry</a></li> |
| <li><a href="http://nodejs.org/api/" class="docs current">Docs</a></li> |
| <li><a href="http://blog.nodejs.org" class="blog">Blog</a></li> |
| <li><a href="/community/" class="community">Community</a></li> |
| <li><a href="/logos/" class="logos">Logos</a></li> |
| <li><a href="http://jobs.nodejs.org/" class="jobs">Jobs</a></li> |
| </ul> |
| <p class="twitter"><a href="http://twitter.com/nodejs">@nodejs</a></p> |
| </div> |
| |
| <div id="column1" class="interior"> |
| <header> |
| <h1>Node.js v0.10.24 Manual & Documentation</h1> |
| <div id="gtoc"> |
| <p> |
| <a href="index.html" name="toc">Index</a> | |
| <a href="all.html">View on single page</a> | |
| <a href="path.json">View as JSON</a> |
| </p> |
| </div> |
| <hr> |
| </header> |
| |
| <div id="toc"> |
| <h2>Table of Contents</h2> |
| <ul> |
| <li><a href="#path_path">Path</a><ul> |
| <li><a href="#path_path_normalize_p">path.normalize(p)</a></li> |
| <li><a href="#path_path_join_path1_path2">path.join([path1], [path2], [...])</a></li> |
| <li><a href="#path_path_resolve_from_to">path.resolve([from ...], to)</a></li> |
| <li><a href="#path_path_relative_from_to">path.relative(from, to)</a></li> |
| <li><a href="#path_path_dirname_p">path.dirname(p)</a></li> |
| <li><a href="#path_path_basename_p_ext">path.basename(p, [ext])</a></li> |
| <li><a href="#path_path_extname_p">path.extname(p)</a></li> |
| <li><a href="#path_path_sep">path.sep</a></li> |
| <li><a href="#path_path_delimiter">path.delimiter</a></li> |
| </ul> |
| </li> |
| </ul> |
| |
| </div> |
| |
| <div id="apicontent"> |
| <h1>Path<span><a class="mark" href="#path_path" id="path_path">#</a></span></h1> |
| <pre class="api_stability_3">Stability: 3 - Stable</pre><p>This module contains utilities for handling and transforming file |
| paths. Almost all these methods perform only string transformations. |
| The file system is not consulted to check whether paths are valid. |
| |
| </p> |
| <p>Use <code>require('path')</code> to use this module. The following methods are provided: |
| |
| </p> |
| <h2>path.normalize(p)<span><a class="mark" href="#path_path_normalize_p" id="path_path_normalize_p">#</a></span></h2> |
| <p>Normalize a string path, taking care of <code>'..'</code> and <code>'.'</code> parts. |
| |
| </p> |
| <p>When multiple slashes are found, they're replaced by a single one; |
| when the path contains a trailing slash, it is preserved. |
| On Windows backslashes are used. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>path.normalize('/foo/bar//baz/asdf/quux/..') |
| // returns |
| '/foo/bar/baz/asdf'</code></pre> |
| <h2>path.join([path1], [path2], [...])<span><a class="mark" href="#path_path_join_path1_path2" id="path_path_join_path1_path2">#</a></span></h2> |
| <p>Join all arguments together and normalize the resulting path. |
| |
| </p> |
| <p>Arguments must be strings. In v0.8, non-string arguments were |
| silently ignored. In v0.10 and up, an exception is thrown. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>path.join('/foo', 'bar', 'baz/asdf', 'quux', '..') |
| // returns |
| '/foo/bar/baz/asdf' |
| |
| path.join('foo', {}, 'bar') |
| // throws exception |
| TypeError: Arguments to path.join must be strings</code></pre> |
| <h2>path.resolve([from ...], to)<span><a class="mark" href="#path_path_resolve_from_to" id="path_path_resolve_from_to">#</a></span></h2> |
| <p>Resolves <code>to</code> to an absolute path. |
| |
| </p> |
| <p>If <code>to</code> isn't already absolute <code>from</code> arguments are prepended in right to left |
| order, until an absolute path is found. If after using all <code>from</code> paths still |
| no absolute path is found, the current working directory is used as well. The |
| resulting path is normalized, and trailing slashes are removed unless the path |
| gets resolved to the root directory. Non-string arguments are ignored. |
| |
| </p> |
| <p>Another way to think of it is as a sequence of <code>cd</code> commands in a shell. |
| |
| </p> |
| <pre><code>path.resolve('foo/bar', '/tmp/file/', '..', 'a/../subfile')</code></pre> |
| <p>Is similar to: |
| |
| </p> |
| <pre><code>cd foo/bar |
| cd /tmp/file/ |
| cd .. |
| cd a/../subfile |
| pwd</code></pre> |
| <p>The difference is that the different paths don't need to exist and may also be |
| files. |
| |
| </p> |
| <p>Examples: |
| |
| </p> |
| <pre><code>path.resolve('/foo/bar', './baz') |
| // returns |
| '/foo/bar/baz' |
| |
| path.resolve('/foo/bar', '/tmp/file/') |
| // returns |
| '/tmp/file' |
| |
| path.resolve('wwwroot', 'static_files/png/', '../gif/image.gif') |
| // if currently in /home/myself/node, it returns |
| '/home/myself/node/wwwroot/static_files/gif/image.gif'</code></pre> |
| <h2>path.relative(from, to)<span><a class="mark" href="#path_path_relative_from_to" id="path_path_relative_from_to">#</a></span></h2> |
| <p>Solve the relative path from <code>from</code> to <code>to</code>. |
| |
| </p> |
| <p>At times we have two absolute paths, and we need to derive the relative |
| path from one to the other. This is actually the reverse transform of |
| <code>path.resolve</code>, which means we see that: |
| |
| </p> |
| <pre><code>path.resolve(from, path.relative(from, to)) == path.resolve(to)</code></pre> |
| <p>Examples: |
| |
| </p> |
| <pre><code>path.relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb') |
| // returns |
| '..\\..\\impl\\bbb' |
| |
| path.relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb') |
| // returns |
| '../../impl/bbb'</code></pre> |
| <h2>path.dirname(p)<span><a class="mark" href="#path_path_dirname_p" id="path_path_dirname_p">#</a></span></h2> |
| <p>Return the directory name of a path. Similar to the Unix <code>dirname</code> command. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>path.dirname('/foo/bar/baz/asdf/quux') |
| // returns |
| '/foo/bar/baz/asdf'</code></pre> |
| <h2>path.basename(p, [ext])<span><a class="mark" href="#path_path_basename_p_ext" id="path_path_basename_p_ext">#</a></span></h2> |
| <p>Return the last portion of a path. Similar to the Unix <code>basename</code> command. |
| |
| </p> |
| <p>Example: |
| |
| </p> |
| <pre><code>path.basename('/foo/bar/baz/asdf/quux.html') |
| // returns |
| 'quux.html' |
| |
| path.basename('/foo/bar/baz/asdf/quux.html', '.html') |
| // returns |
| 'quux'</code></pre> |
| <h2>path.extname(p)<span><a class="mark" href="#path_path_extname_p" id="path_path_extname_p">#</a></span></h2> |
| <p>Return the extension of the path, from the last '.' to end of string |
| in the last portion of the path. If there is no '.' in the last portion |
| of the path or the first character of it is '.', then it returns |
| an empty string. Examples: |
| |
| </p> |
| <pre><code>path.extname('index.html') |
| // returns |
| '.html' |
| |
| path.extname('index.') |
| // returns |
| '.' |
| |
| path.extname('index') |
| // returns |
| ''</code></pre> |
| <h2>path.sep<span><a class="mark" href="#path_path_sep" id="path_path_sep">#</a></span></h2> |
| <p>The platform-specific file separator. <code>'\\'</code> or <code>'/'</code>. |
| |
| </p> |
| <p>An example on *nix: |
| |
| </p> |
| <pre><code>'foo/bar/baz'.split(path.sep) |
| // returns |
| ['foo', 'bar', 'baz']</code></pre> |
| <p>An example on Windows: |
| |
| </p> |
| <pre><code>'foo\\bar\\baz'.split(path.sep) |
| // returns |
| ['foo', 'bar', 'baz']</code></pre> |
| <h2>path.delimiter<span><a class="mark" href="#path_path_delimiter" id="path_path_delimiter">#</a></span></h2> |
| <p>The platform-specific path delimiter, <code>;</code> or <code>':'</code>. |
| |
| </p> |
| <p>An example on *nix: |
| |
| </p> |
| <pre><code>console.log(process.env.PATH) |
| // '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin' |
| |
| process.env.PATH.split(path.delimiter) |
| // returns |
| ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']</code></pre> |
| <p>An example on Windows: |
| |
| </p> |
| <pre><code>console.log(process.env.PATH) |
| // 'C:\Windows\system32;C:\Windows;C:\Program Files\nodejs\' |
| |
| process.env.PATH.split(path.delimiter) |
| // returns |
| ['C:\Windows\system32', 'C:\Windows', 'C:\Program Files\nodejs\']</code></pre> |
| |
| </div> |
| </div> |
| </div> |
| <div id="footer"> |
| <a href="http://joyent.com" class="joyent-logo">Joyent</a> |
| <ul class="clearfix"> |
| <li><a href="/">Node.js</a></li> |
| <li><a href="/download/">Download</a></li> |
| <li><a href="/about/">About</a></li> |
| <li><a href="http://npmjs.org/">npm Registry</a></li> |
| <li><a href="http://nodejs.org/api/">Docs</a></li> |
| <li><a href="http://blog.nodejs.org">Blog</a></li> |
| <li><a href="/community/">Community</a></li> |
| <li><a href="/logos/">Logos</a></li> |
| <li><a href="http://jobs.nodejs.org/">Jobs</a></li> |
| <li><a href="http://twitter.com/nodejs" class="twitter">@nodejs</a></li> |
| </ul> |
| |
| <p>Copyright <a href="http://joyent.com/">Joyent, Inc</a>, Node.js is a <a href="/trademark-policy.pdf">trademark</a> of Joyent, Inc. View <a href="https://raw.github.com/joyent/node/v0.10.24/LICENSE">license</a>.</p> |
| </div> |
| |
| <script src="../sh_main.js"></script> |
| <script src="../sh_javascript.min.js"></script> |
| <script>highlight(undefined, undefined, 'pre');</script> |
| <script> |
| window._gaq = [['_setAccount', 'UA-10874194-2'], ['_trackPageview']]; |
| (function(d, t) { |
| var g = d.createElement(t), |
| s = d.getElementsByTagName(t)[0]; |
| g.src = '//www.google-analytics.com/ga.js'; |
| s.parentNode.insertBefore(g, s); |
| }(document, 'script')); |
| </script> |
| </body> |
| </html> |
| |