Node.js Reference ยท path, os and url
Node.js path.sep and path.delimiter
What path.sep and path.delimiter are in Node.js: the folder separator and the PATH list separator on each platform, with examples of splitting PATH correctly.
path.sep is the character that separates folders inside one path: / on Linux and macOS, \ on Windows. path.delimiter is the character that separates several paths inside one string, such as the PATH environment variable: : on Linux and macOS, ; on Windows.
The two are easy to mix up. One divides the parts of a single path (/usr/bin), the other divides a list of paths (/usr/bin:/bin). Both are plain strings of length one.
import path from "node:path"; path.sep; // "/" (POSIX) or "\\" (Windows) path.delimiter; // ":" (POSIX) or ";" (Windows)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
What the constants are for
path.sepis for splitting paths into segments or for checks such asstartsWith(base + path.sep). For building paths, preferpath.join(), which also normalizes.path.delimiteris for reading and writing lists of folders, such asPATH,NODE_PATHor a--includesetting that accepts several folders.
Both come in three forms: the plain constant (matches the platform running the code), and the explicit path.posix.sep, path.win32.sep, path.posix.delimiter and path.win32.delimiter. See path.posix and path.win32.
Usage notes
- Windows accepts both separators in input but writes
\in output. So"a/b".split(path.sep)does not split on Windows, whilenormalizefirst would. - Splitting a path with
sepyields empty strings for a leading or doubled separator ("/a".split("/")gives["", "a"]). Filter them out or normalize first. - Do not hard-code
/or:if the code may run on Windows. APATHsplit by:breaks on drive letters likeC:. - URLs always use
/. Do not usepath.sepfor web addresses, usepath.posix. - Prefix checks need the separator.
"/srv/app2".startsWith("/srv/app")istrue, which is wrong for a folder test. Compare withbase + path.sepor usepath.relative(). - File names can contain the other platform's separator. A backslash is a legal character in POSIX file names, so do not treat it as a separator there.
process.env.PATHmay be calledPathon Windows (the variable name is case-insensitive there), but the value always uses;.