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.

Share:
14,792

Related videos on Youtube

ciembor
Author by

ciembor

Updated on June 19, 2022

Comments

  • ciembor
    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 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?

  • jcubic
    jcubic over 3 years
    this and __filename it's file and directory of a module loaded with require not necessary executable path.