DEV Community

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

Posted on

10

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!!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (1)

Collapse
 
ibn21d profile image
Indu Bhusan Nath

Great info.

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more