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

Viewed 951

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.

4 Answers

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

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

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

try this and see if it helps (link):

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

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