Getting directory from which node.js was executed
14,792
Solution 1
process.cwd()
will provide that.
Solution 2
__dirname
gives you the path where a file resides.
Related videos on Youtube
Author by
ciembor
Updated on June 19, 2022Comments
-
ciembor about 2 years
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 frommain.js
, but also from any submodule. I tried withpath
plugin and__directory
, but I get a path of the current file (for example submodule). I also triedrequire('path').dirname(require.main.filename)
, but when I runmake test
I get mocha dirname instead of my project directory. What is the easiest way to solve this? -
jcubic over 3 yearsthis and
__filename
it's file and directory of a module loaded with require not necessary executable path.