Languages
[Edit]
EN

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

0 points
Created by:
Anisha-Kidd
322

In this article, we would like to show you how to split path to directories array with file names 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');

// example path
const myPath = 'C:\\projects\\app\\index.js';

// split path to directories array
const result = myPath.split(path.sep);

console.log(result);

Output:

[ 'C:', 'projects', 'app', 'index.js' ]

References

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