To get the name of all the files from a directory in nodejs, we can use the readdirSync()
or readdir()
functions from the native filesystem (fs
) module in Node.js.
/* Get name of files in a directory */
const fileNames = readdirSync("myFolder");
- The
readdirSync()
function is used to synchronously read the files which further blocks the execution of code in Nodejs. - The
readdir()
function is used to asynchronously read the files in Node.js.
For example, Let's say we have 20 files in a folder or directory called myFiles
and we need to get all the names of files in that folder.
First thing we have to do is require()
or import
the filesystem
module like this,
// get filesystem module
const fs = require("fs");
Using readdirSync() - Synchronous function
Now we can use the readdirSync()
function from the fs
module and pass the path of the directory.
It can be done like this,
// get filesystem module
const fs = require("fs");
// using the readdirSync() function
// and passing the path of the directory as argument
const fileNames = fs.readdirSync("myFiles");
console.log(fileNames);
/*
[
'file_1.txt', 'file_10.txt',
'file_11.txt', 'file_12.txt',
'file_13.txt', 'file_14.txt',
'file_15.txt', 'file_16.txt',
'file_17.txt', 'file_18.txt',
'file_19.txt', 'file_2.txt',
'file_20.txt', 'file_3.txt',
'file_4.txt', 'file_5.txt',
'file_6.txt', 'file_7.txt',
'file_8.txt', 'file_9.txt'
]
*/
- The
readdirSync()
function returns an array of file names. - Since the
myFiles
directory is in the same folder as the running script, we don't need to resolve the path. But sometimes you need to resolve the path because the directory and the script may not be in the same directory, for that you can use thepath
module in Node.js to achieve that.
See this example live in repl.it.
Using readdir() - Asynchronous function
Now let's use the readdir()
function from the fs
module.
Here the function needs 2 arguments,
- The First argument is the path to the directory
- The second one is an error first callback function which will get executed when all the names of the files are read. The callback function receives an
error
object as the first parameter if any, and an array of filenames as the second parameter.
It can be done like this,
// get filesystem module
const fs = require("fs");
// using the readdir() function
// and passing the path to the directory
// as the first argument
// callback function to execute after file read
// as the second argument
fs.readdir("myFiles", (err, fileNames) => {
// if any error
if (err) {
console.error(err);
return;
}
// otherwise show the filenames
console.log(fileNames);
/*
[
'file_1.txt', 'file_10.txt',
'file_11.txt', 'file_12.txt',
'file_13.txt', 'file_14.txt',
'file_15.txt', 'file_16.txt',
'file_17.txt', 'file_18.txt',
'file_19.txt', 'file_2.txt',
'file_20.txt', 'file_3.txt',
'file_4.txt', 'file_5.txt',
'file_6.txt', 'file_7.txt',
'file_8.txt', 'file_9.txt'
]
*/
});
See this example live in repl.it.
And this is how we get all our file names from a directory.