91

I have an application which uploads csv file into a particular folder, say, "uploads". Now I want to get the full path of that csv file, for example, D:\MyNodeApp\uploads\Test.csv.

How do I get the file location in Node.js? I used multer to upload file.

1
  • 1
    Need to know what info you have to start with. Do you already have the filename without the path? Do all files go into the same folder? Is this folder a known location at run time? A code example may help here. Commented Jul 9, 2015 at 12:16

6 Answers 6

215
var path = require("path"); // OR: import { resolve } from '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 this:

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I feel like this should be the accepted answer, as it is a lot more helpful and practical than the other answers.
You saved my day, after trying out many different solutions to accomplish this simple task (getting absolute path) your answer worked finally
16

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);

});

Comments

7

Assuming you are using multer with express, try this in your controller method:

var path = require('path');

//gets your app's root path
var root = path.dirname(require.main.filename)

// joins uploaded file path with root. replace filename with your input field name
var absolutePath = path.join(root,req.files['filename'].path) 

Comments

6

In TypeScript, I did the following based on the relative file path.

import { resolve } from 'path';

public getValidFileToUpload(): string {
  return resolve('src/assets/validFilePath/testFile.csv');
}

1 Comment

This is also what you would use in a JS project that uses ESM modules.
4

If you are using a "dirent" type:

const path = require( "path" );
full_path = path.resolve( path_to_folder_containing__dir_ent__ , dir_ent.name );

Class: fs.Dirent https://nodejs.org/api/fs.html#fs_dirent_name

Module: fs.path https://nodejs.org/api/path.html

Comments

3

I think the simplest option is: dirname module: https://nodejs.org/docs/latest/api/modules.html#modules_dirname

filename: https://nodejs.org/docs/latest/api/modules.html#modules_filename

console.log(__dirname)

console.log(__filename)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.