Languages
[Edit]
EN

Node.js - get platform-specific path segment separator

3 points
Created by:
Paris-Bateman
434

In this article, we would like to show you how to get a 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

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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