__dirname in Node.js returns the directory of the current file

2018-09-09

Official documentation: https://nodejs.org/docs/latest/api/modules.html#modules_dirname

Notes:

  • This is a different thing than process.cwd(), as the current file directory and the current working directory may not be necessarily the same.
  • This works when executing Node.js files but not on the Node.js console. I guess that’s because in the latter case the code is not being executed from within a file.