Categories
JavaScript Answers

How to convert a directory structure in the filesystem to JSON with Node.js?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *