list all the files in the current directory in javascript

To list all the files in the current directory in Node.js, you can use the built-in fs (file system) module. Here's an example code:

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

// Read the current directory
fs.readdir('.', (err, files) => {
  if (err) throw err;
  // Log the files
  console.log(files);
});
158 chars
9 lines

The readdir function takes two arguments: the first one is the path to the directory you want to read (in this case, . represents the current directory), and the second one is a callback function that will be called with two arguments: err (an error, if any occurred) and files (an array of file names in the directory). In this example, we simply log the files array to the console.

related categories

gistlibby LogSnag