Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
How to get an array of files in a directory

In PHP I have the following code to create an array of all images in a directory.

$dir    = '/public/images/';
$files = scandir($dir);

What would the NodeJS equivalent be?

I am looking to create an array of all images in a directory.

Using this in Laravel webpack mix in development so it would run either on the local machine or on the server itself by command line.

Instead of the following as there is an unknown number of images in the directory and what the name would be.

if (mix.config.inProduction) {
    mix.version(['public/images/50x50-1.png', 'public/images/50x50-1.svg'])
}

Desired Code:

if (mix.config.inProduction) {
    var imagesArray = // get the list here
    mix.version(imagesArray)
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If this is running on the server, you should have access to node's fs module.

const testFolder = '/public/images/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
  files.forEach(file => {
    console.log(file);
  });
})

You can read up on fs here

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!