How do I get just the names of files in a folder in js

I’m trying to make a file viewer for my website. I’m wondering if it’s possible to make a js script that gets all the file paths within a folder. I tried to search it up but I only found ones that are for node.js but I’m looking for one that works with regular JavaScript in a website. Please help!

I don’t believe that would be possible. To create such a script, the code would need access to the file system of the server, which in-browser js doesn’t have. I’m sure there could be some sort of workaround if you configured a webserver like apache/nginx to list directories, then parse that directory, but that is quite tricky. Why not try node?

I feel like there is a better way instead of moving all my files to node.js. It’s not supposed to be a big addition or anything it’s just a small gag for something

There is no concept of a “directory” in browser land (URLs are more like files in that you access a URL, and then you get some data back), so there is no way to ask for a “list of all files in this directory” using client-side JS, unfortunately. What you can do though is create a little file called files.json (or something similar) that you regenerate periodically, with all the file names necessary, That way you can load that file using fetch("files.json").then(res => res.json()).then(fileList => { ... });

However, if you need true “directory listing” then you’ll need to run your content with a server so that you can create a special URL that acts as the thing that generates the files.json content on request based on what the file system looks like at that moment in time.

2 Likes