To convert a directory structure in the filesystem to JSON with Node.js, we use the directory-tree
package.
To install it, we run
npm i directory-tree
Then we use it by writing
const directoryTree = require("directory-tree");
const tree = directoryTree("/some/path");
const filteredTree = directoryTree("/some/path", [".jpg", ".png"]);
to call directoryTree
to return an object of the directory structure with the /some/path folder as root.
We can also call it with a 2nd argument to filter the items by extension.