__dirname

__dirname

  • {String}

The name of the directory that the currently executing script resides in.

Example: running node example.js from /Users/mjr

console.log(__dirname);
// /Users/mjr

__dirname isn't actually a global but rather local to each module.

For instance, given two modules: a and b, where b is a dependency of a and there is a directory structure of:

  • /Users/mjr/app/a.js
  • /Users/mjr/app/node_modules/b/b.js

References to __dirname within b.js will return /Users/mjr/app/node_modules/b while references to __dirname within a.js will return /Users/mj/app.

doc_Nodejs
2016-04-30 04:43:44
Comments
Leave a Comment

Please login to continue.