find the contents of a directory in javascript

To find the contents of a directory in Javascript, you can use the readdir method of the fs module, which is built into Node.js. Here is an example of how you can use it:

index.tsx
const fs = require('fs');

fs.readdir('/path/to/directory', (err, files) => {
  if (err) {
    console.error(err);
    return;
  }

  files.forEach(file => {
    console.log(file);
  });
});
191 chars
13 lines

In this example, readdir takes two parameters: the path to the directory you want to read, and a callback function that will be called with two arguments: an error object (if an error occurred while reading the directory), and an array of file and directory names in the directory.

Since readdir is an asynchronous method, it uses a callback function to handle the results. In this example, we log the name of each file and directory in the directory to the console.

gistlibby LogSnag