DEV Community

Cover image for How to go back directory in Nodejs?
Blue
Blue

Posted on

How to go back directory in Nodejs?

If you are new to Nodejs , you may not know how to use file module completely. I hope this article may help you to find out ways to create better application with Nodejs.

Nodejs is really fast than other languages' processing .All javascript lovers are now fall in love with nodejs because of its non-blocking feature and robust architecture .

Today ,I am going to show you how we can go back file directory in nodejs . Now ,we will log in console to know where we are running ,using__dirname.


console.log(__dirname);//current directory where we open this folder.
Enter fullscreen mode Exit fullscreen mode

Now, we get our current directory but you are trying to move your directory one step back.You must use nodejs built in module path to work with directories. The method path.join() execute several segments into one path.


const path=require('path');
let oneStepBack=path.join(__dirname,'../');
console.log(oneStepBack);//move one step back from current directory

Enter fullscreen mode Exit fullscreen mode

If you want to move two step backward from current working directory, the code will be like this...


const path=require('path');
let twoStepBack=path.join(__dirname,'../../');
console.log(twoStepBack);//move two step back from current directory

Enter fullscreen mode Exit fullscreen mode

Happy coding!!

Top comments (1)

Collapse
 
ibn21d profile image
Indu Bhusan Nath

Great info.