Getting directory from which node.js was executed

ciembor picture ciembor · Mar 1, 2013 · Viewed 13.2k times · Source

I have some project, and I run it with node main.js / make test etc. What I need is to get this directory from a script. Not only from main.js, but also from any submodule. I tried with path plugin and __directory, but I get a path of the current file (for example submodule). I also tried require('path').dirname(require.main.filename), but when I run make test I get mocha dirname instead of my project directory. What is the easiest way to solve this?

Answer

JohnnyHK picture JohnnyHK · Mar 1, 2013

process.cwd() will provide that.