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:
npm install --save @types/node
2. Import path module using:
import * as path from 'path';
3. Use string split() method with path.sep argument.
Note:
The
path.sepprovides the platform-specific path segment separator:
\on Windows/on POSIX
Practical example
import * as path from 'path';
const indexPath: string = 'C:\\projects\\app\\index.js'; // example path
const pathParts: string[] = indexPath.split(path.sep); // directories and file names array
console.log(pathParts);
Output:
[ 'C:', 'projects', 'app', 'index.js' ]