EN
TypeScript / Node.js - get platform-specific path segment separator
0 points
In this article, we would like to show you how to get a platform-specific path segment separator in Node.js.
Quick solution:
xxxxxxxxxx
1
import * as path from 'path';
2
3
console.log(path.sep); // \ <-- on windows
4
// / <-- on Linux/Unix (POSIX)
1. Install node types with the following command:
xxxxxxxxxx
1
npm install --save @types/node
2. Import path
module using:
xxxxxxxxxx
1
import * as path from 'path';
3. 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)
4. Motivation: having access to a separator you can do platform-independent operations on the paths,
e.g. filePath.split(path.sep)
.
xxxxxxxxxx
1
import * as path from 'path';
2
3
console.log(path.sep);
Output (on Windows):
xxxxxxxxxx
1
\