EN
TypeScript / Node.js - split path to directories array with file names
0 points
In this article, we would like to show you how to split path to directories array with file name in Node.js.
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. Use string split()
method with path.sep
argument.
Note:
The
path.sep
provides the platform-specific path segment separator:
\
on Windows/
on POSIX
xxxxxxxxxx
1
import * as path from 'path';
2
3
const indexPath: string = 'C:\\projects\\app\\index.js'; // example path
4
const pathParts: string[] = indexPath.split(path.sep); // directories and file names array
5
6
console.log(pathParts);
Output:
xxxxxxxxxx
1
[ 'C:', 'projects', 'app', 'index.js' ]