Working with Path Module

1. Joining Paths (path.join)

Example

import path from "node:path";
path.join("/foo", "bar", "baz/asdf", "..", "qux"); // "/foo/bar/baz/qux"

2. Resolving Absolute Paths (path.resolve)

BehaviorDetail
Right-to-leftStops at first absolute path
Uses process.cwd()If no absolute path supplied

3. Getting Directory Name (path.dirname)

InputOutput
/a/b/c.js/a/b
foo.

4. Getting Base Name (path.basename)

CallReturns
basename("/a/b/c.js")c.js
basename("/a/b/c.js", ".js")c

5. Getting File Extension (path.extname)

InputOutput
index.js.js
archive.tar.gz.gz
.hidden""

6. Normalizing Paths (path.normalize)

InputOutput
/foo/bar//baz/qux/../foo/bar/baz

7. Checking Absolute Paths (path.isAbsolute)

PathResult
/footrue (POSIX)
C:\footrue (Windows)
./foofalse

8. Getting Relative Paths (path.relative)

Example

path.relative("/data/orandea/test/aaa", "/data/orandea/impl/bbb"); // "../../impl/bbb"

9. Parsing Paths (path.parse)

FieldExample
root/
dir/home/user
basefile.txt
namefile
ext.txt

10. Formatting Paths (path.format)

Input ObjectOutput
{ dir: "/foo", base: "bar.js" }/foo/bar.js
{ name: "x", ext: ".y" }x.y

11. Using Platform-Specific Separators

PropertyPOSIXWindows
path.sep/\
path.delimiter:;
path.posix.*Force POSIX
path.win32.*Force Windows