You can use the [`fs.readdir`][1] or [`fs.readdirSync`][2] methods.
**fs.readdir**
const testFolder = './tests/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file);
});
});
**fs.readdirSync**
const testFolder = './tests/';
const fs = require('fs');
fs.readdirSync(testFolder).forEach(file => {
console.log(file);
});
The difference between the two methods, is that the first one is asynchronous, so you have to provide a callback function that will be executed when the read process ends.
The second is synchronous, it will return the file name array, but it will stop any further execution of your code until the read process ends.
[1]: https://nodejs.org/api/fs.html#fs_fs_readdir_path_options_callback
[2]: https://nodejs.org/api/fs.html#fs_fs_readdirsync_path_options
You can use the [`fs.readdir`][1] or [`fs.readdirSync`][2] methods.
**fs.readdir**
const testFolder = './tests/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file);
});
});
**fs.readdirSync**
const testFolder = './tests/';
const fs = require('fs');
fs.readdirSync(testFolder).forEach(file => {
console.log(file);
});
The difference between the two methods, is that the first one is asynchronous, so you have to provide a callback function that will be executed when the read process ends.
The second is synchronous, it will return the file name array, but it will stop any further execution of your code until the read process ends.
[1]: https://nodejs.org/api/fs.html#fs_fs_readdir_path_options_callback
[2]: https://nodejs.org/api/fs.html#fs_fs_readdirsync_path_options