path.delimiter

Provides the platform-specific delimiter used between entries in PATH-style variables.

Signature
path.delimiter
  • Added in Node.js v0.9.3

Return value

stringThe platform-specific path delimiter: ';' on Windows and ':' on POSIX.

path.delimiter is the character that separates entries in a list of paths, as opposed to path.sep, which separates segments within one path. It is ':' on POSIX and ';' on Windows.

The distinction exists because Windows uses : inside paths for drive letters, so it could not also use it as the list delimiter. That is the whole reason a separate constant is needed.

When to reach for it

Any variable holding several paths in one string: PATH, NODE_PATH, MANPATH, and the --allow-fs-read style lists some tools accept. Splitting on a hardcoded ':' is a portability bug that shreds every Windows path at its drive letter.

What trips people up

Real PATH values are messier than the examples suggest. Empty entries appear from stray leading, trailing or doubled delimiters, and an empty entry historically means "the current directory" — usually not what you want, so filter them out. Entries may also be quoted or contain spaces, and on Windows they may be enclosed in double quotes that you must strip yourself.

process.env.PATH can also be undefined in a stripped environment, so guard before calling .split() on it. And note that PATH is case-insensitive on Windows, where the variable is often spelled Path — process.env handles that lookup for you, but code reading process.env keys directly will not.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Listing the directories a command would be searched for in
  • Appending a directory to PATH before handing an environment to a child process
  • Parsing any environment variable that holds several paths in one string

Frequently asked questions

How do I split the PATH environment variable in Node.js?

Split on path.delimiter: (process.env.PATH ?? '').split(path.delimiter).filter(Boolean). That is ':' on POSIX and ';' on Windows. Filtering removes empty entries produced by stray delimiters, which would otherwise be interpreted as the current directory.

What is the difference between path.sep and path.delimiter?

path.sep separates directory names inside one path ('/' or '\'), while path.delimiter separates whole paths in a list such as PATH (':' or ';'). Windows needs two different characters because ':' already appears inside paths after a drive letter.

Is path.delimiter always a colon?

No. It is ':' on POSIX systems and ';' on Windows, which is why hardcoding a colon breaks Windows PATH parsing — every entry gets cut at its drive letter. Use path.posix.delimiter or path.win32.delimiter when you need a specific platform's value.

More examples

Appending a directory to PATH portably

Splitting and rejoining on path.delimiter keeps the string valid on either platform, and filtering falsy entries removes any empty slots the original value carried. This is the standard preparation for an env option passed to a child process.

Example · javascript
const path = require('node:path');

const current = process.env.PATH ?? '';
const extra = path.resolve('node_modules', '.bin');

const entries = current.split(path.delimiter).filter(Boolean);
const updated = [extra, ...entries].join(path.delimiter);

console.log('added entry is absolute:', path.isAbsolute(extra));
console.log('now first:', updated.split(path.delimiter)[0] === extra);
console.log('entry count grew by 1:', updated.split(path.delimiter).length === entries.length + 1);

Why a hardcoded colon breaks on Windows

Splitting on a literal ':' cuts each entry apart at its drive letter and produces meaningless fragments. Using the platform delimiter is the only correct approach, and this is exactly the bug path.delimiter exists to prevent.

Example · javascript
const path = require('node:path');

const windowsPath = 'C:\\Windows\\system32;C:\\Program Files\\node';

console.log('hardcoded colon:', windowsPath.split(':'));
// -> [ 'C', '\\Windows\\system32;C', '\\Program Files\\node' ]  -- destroyed

console.log('win32 delimiter:', windowsPath.split(path.win32.delimiter));
// -> [ 'C:\\Windows\\system32', 'C:\\Program Files\\node' ]

delimiter separates paths, sep separates segments

The outer split uses delimiter to get individual paths; the inner split uses sep to break each path into directory names. Mixing the two up is the usual cause of 'why is my PATH one giant entry' confusion.

Example · javascript
const path = require('node:path');

const list = ['/usr/local/bin', '/usr/bin'].join(path.posix.delimiter);
console.log('the list:', list);

for (const entry of list.split(path.posix.delimiter)) {
  console.log(entry.padEnd(16), '-> segments:', entry.split(path.posix.sep).filter(Boolean));
}

Discussion

  • Be the first to comment on this lesson.