Categories
JavaScript Answers

How to get the parent directory name in Node.js?

Spread the love

Sometimes, we want to get the parent directory name in Node.js.

In this article, we’ll look at how to get the parent directory name in Node.js.

How to get the parent directory name in Node.js?

To get the parent directory name in Node.js, we can use the path.basename and path.dirname methods.

For instance, we write

path.basename(path.dirname(filename))

to get the folder name of the path of filename with path.dirname.

Then we call path.basename to get the parent folder path from the path string returned by path.dirname.

Conclusion

To get the parent directory name in Node.js, we can use the path.basename and path.dirname methods.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *