How to get folder path from a file with Node.js?

Sometimes, we want to get folder path from a file with Node.js.

In this article, we’ll look at how to get folder path from a file with Node.js.

How to get folder path from a file with Node.js?

To get folder path from a file with Node.js, we can use the path.dirname method.

For instance, we write

const path = require('path');
const folder = path.dirname('G:\node-demos\7-node-module\demo\config.json')

to call path.dirname with the path string we want to get the folder path for.

It returns the folder path string which we assign to folder.

Conclusion

To get folder path from a file with Node.js, we can use the path.dirname method.