0

I have couple of images in images/source directory which is at root level. I want to read all the images in the folder and get its path to something like below:

var images = ['images/source/first.jpg', 'images/source/second.png', 'images/source/third.png'];

I am trying to read the directory something like below, but its not working!

const imgPath= fs.readdirSync(path.join(__dirname, '/images/source')).sort();

The above code reads all the images, but i need all the images with their path in an array.

ShankarGuru
  • 627
  • 1
  • 6
  • 17
  • Does this answer your question? [How do you get a list of the names of all files present in a directory in Node.js?](https://stackoverflow.com/questions/2727167/how-do-you-get-a-list-of-the-names-of-all-files-present-in-a-directory-in-node-j) – Brahma Dev Jun 10 '21 at 17:43
  • const imgPath= fs.readdirSync(path.join(__dirname, '/images/source')).sort().map(e => `images/source/${e}`) just map the array? – Shahriar Shojib Jun 10 '21 at 17:46

1 Answers1

0

Read this forum. It may helpful.

How do you get a list of the names of all files present in a directory in Node.js?

const testFolder = './tests/';
const fs = require('fs');

fs.readdir(testFolder, (err, files) => {
  files.forEach(file => {
    console.log(file);
  });
});