Raghul SK
Raghul SK

Reputation: 1390

How to set the entire path name into the variable in NodeJS?

I am working on nodejs. I have to set my entire pathname into the variable.

I tried this to set the path name in to the variable

var secretKey = path.basename("../Users/secretkey.pem");

when I console.log the variable I'll get only the filename secretkey.pem alone but not get the exact path that I mentioned.

How to Set the Exact path into the variable.

Upvotes: 2

Views: 1692

Answers (4)

Ashutosh Kumar
Ashutosh Kumar

Reputation: 907

Well if you need the absolute path of the secretkey.

You can do something like this

var filename = "secretkey.pem";
var fullpath = __dirname + "/Users/" + filename;

Upvotes: 0

Peter
Peter

Reputation: 2927

From Node documentation | Path:

The path.basename() method returns the last portion of a path

which is exactly what you report as the output in your console.

Try the following:

const file_name = path.resolve(path.join(__dirname, '..', 'Users/secretkey.pem'));

Upvotes: 2

outlandish
outlandish

Reputation: 92

try this and see if it helps (link):

var filename = path.resolve('../Users/secretkey.pem');

Upvotes: 0

Muhammad Saqlain
Muhammad Saqlain

Reputation: 2202

You can use resolve. It Resolves the specified paths into an absolute path.

var fullpath = path.resolve("../Users/secretkey.pem");

Upvotes: 2

Related Questions