Skip to content
Tutorials Center
My learning

Topics

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.

NODE
import path from "node:path";

path.sep;         // "/" (POSIX)  or "\\" (Windows)
path.delimiter;   // ":" (POSIX)  or ";" (Windows)

Try it

Node.js The two constants side by side

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.

Node.js Working with a PATH-style variable

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.sep is for splitting paths into segments or for checks such as startsWith(base + path.sep). For building paths, prefer path.join(), which also normalizes.
  • path.delimiter is for reading and writing lists of folders, such as PATH, NODE_PATH or a --include setting 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, while normalize first would.
  • Splitting a path with sep yields 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. A PATH split by : breaks on drive letters like C:.
  • URLs always use /. Do not use path.sep for web addresses, use path.posix.
  • Prefix checks need the separator. "/srv/app2".startsWith("/srv/app") is true, which is wrong for a folder test. Compare with base + path.sep or use path.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.PATH may be called Path on Windows (the variable name is case-insensitive there), but the value always uses ;.