I am trying to assign path to a field in an object using NodeJS & Javascript.
Below is the code that I am using:
console.log("__dirname ===> ", __dirname);
let direc = __dirname;
console.log("direc : ", direct);
let p = {};
p.da = __dirname;
console.log("p: ", p);
Its output is:
__dirname ===> C:\Practice\testingpractice\lessons\one
direc : C:\Practice\testingpractice\lessons\one
p:{ da: "C:\\Practice\\testingpractice\\lessons\\one" }
So, above as you can see, I have printed __dirname which gives correct path name. Have also assigned __dirname
to a new variable and printed. That did not give 2 slashes. But when I assign it to a field of an object there are 2 slashes. In this case it is p.da
which gave C:\\Practice\\testingpractice\\lessons\\one
Output that I expect is :
p:{ da: "C:\Practice\testingpractice\lessons\one" }
I do not expect 2 slashes. I want it a pure path name of the directory. And I want it in that manner only. Not with /. Expectation is with \. The steps and code I want is exact same as above.
I have also tried with regex
. But did not seem to work also.
Below is the code for it:
console.log("__dirname ===> ", __dirname);
let direc = __dirname;
console.log("direc : ", direct);
var regex = /\\/g;
let p = {};
p.da = __dirname.replace(regex,'\\');
console.log("p: ", p);
Gave below output:
__dirname ===> C:\Practice\testingpractice\lessons\one
direc : C:\Practice\testingpractice\lessons\one
p:{ da: "C:\\Practice\\testingpractice\\lessons\\one" }
Both the outputs were same.
Tried to compare the strings, but did not return as equal
if(direc === p.da) {console.log("equal");} else {console.log("not equal");}
Output:
not equal
I don't know where I am going wrong. Please help me understand my mistake if any and appreciate for the solutions. Thanks in advance!