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

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.