node get filename from path code example

Example 1: how to get file name in directory node js

const fs = require('fs')

const dir = '/Users/flavio/folder'
const files = fs.readdirSync(dir)

for (const file of files) {
  console.log(file)
}

Example 2: get file name nodejs

var path = require('path');
var scriptName = path.basename(__filename);

Example 3: node get path of current file

module.filename

Example 4: how to get file name in directory node js

const path = require('path')

//...

//inside the `for` loop
const stat = fs.lstatSync(path.join(dir, file))