Getting directory from which node.js was executed Getting directory from which node.js was executed node.js node.js

Getting directory from which node.js was executed


__dirname gives you the path where a file resides.


require.cache stores every loaded module and its associated imported values, parent module, child modules, absolute file path, etc.

I believe each module is assigned to the cache object using its file path as the cache key, but you will probably want to double check that, just to make sure.

If that is the case though, something as simple as Object.keys(require.cache) will give you an array of file paths for all of the modules. Then just parse each path as needed to pull the directory information you are looking for from each module path.