Languages
[Edit]
EN

Node.js - split path to directories array with file names

3 points
Created by:
Anisha-Kidd
532

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' ]

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