Languages
[Edit]
EN

Node.js - get platform-specific path segment separator

3 points
Created by:
Paris-Bateman
254

In this article, we would like to show you how to get platform-specific path segment separator in Node.js.

Quick solution:

const path = require('path');

console.log(path.sep);  // \  <-- on windows
                        // /  <-- on Linux/Unix (POSIX)

 

Detailed description

1. Import path module using:

const path = require('path');

2. Access to path.sep property that provides the platform-specific path segment separator:

  • \ on Windows (sometimes on Japanese Windows you can see in the path „ used as separator)
  • / on Linux/Unix (POSIX)

3. Motivation: having access to a separator you can do platform-independent operations on the paths,
e.g. filePath.split(path.sep).

Practical example

const path = require('path');

console.log(path.sep);

Output (on Windows):

\

References

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂

Join