EN
Node.js - split path to directories array with file names
3
points
In this article, we would like to show you how to split path to directories array with file name in Node.js.
1. Import path
module using:
const path = require('path');
2. Use string split()
method with path.sep
argument.
Note:
The
path.sep
provides the platform-specific path segment separator:
\
on Windows/
on POSIX
Practical example:
const path = require('path');
const indexPath = 'C:\\projects\\app\\index.js'; // example path
const pathParts = indexPath.split(path.sep); // directories and file names array
console.log(pathParts);
Output:
[ 'C:', 'projects', 'app', 'index.js' ]