How to get a list of all files in a folder using Node.js?
Hello everyone! I'm new to Node.js and writing a small script for image processing. I need to get a list of all files in a specific folder so I can loop through them and perform some actions (e.g., resize or rename).\n\nI tried using `fs.readdir`, but I'm not getting it right. Here's my code:\n\n```javascript\nconst fs = require('fs');\n\nconst folderPath = './images';\nconst files = fs.readdir(folderPath);\nconsole.log(files); // Outputs undefined\n```\n\nI expected to see an array with file names, but I get `undefined`. I read that `fs.readdir` is asynchronous, but I don't fully understand how to work with it correctly. Maybe I need to use a callback or promises?\n\nI'm also interested in how to filter only files, excluding folders, and how to get the list.
No answers yet.
Log in to leave an answer