Get full file path in Node.js

Solution 1:

var path = require("path");
var absolutePath = path.resolve("Relative file path");

You dir structure for example:

C:->WebServer->Public->Uploads->MyFile.csv

and your working directory would be Public for example, path.resolve would be like that.

path.resolve("./Uploads/MyFile.csv");

POSIX home/WebServer/Public/Uploads/MyFile.csv
WINDOWS C:\WebServer\Public\Uploads\MyFile.csv

this solution is multiplatform and allows your application to work on both windows and posix machines.

Solution 2:

Get all files from folder and print each file description.

const path = require( "path" );
const fs = require( 'fs' );
const log = console.log;
const folder = './';

fs.readdirSync( folder ).forEach( file => {
   
   const extname = path.extname( file );
   const filename = path.basename( file, extname );
   const absolutePath = path.resolve( folder, file );

   log( "File : ", file );
   log( "filename : ", filename );
   log( "extname : ", extname );
   log( "absolutePath : ", absolutePath);

});