Warm tip: This article is reproduced from stackoverflow.com, please click
directory javascript node.js path pwd

Getting directory from which node.js was executed

发布于 2020-04-13 10:50:33

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?

Questioner
ciembor
Viewed
51
12.1k 2017-04-28 04:23

process.cwd() will provide that.