Is there a more transparent way to access the root of a Node.js application besides process.cwd()? - I am aware this is pretty transparent but I am just curious for those who may pick up the application later on and don't know what cwd() might infer.
To clarify - if my applicaiton lives in /var/www/node-app, I want to access that path from /var/www/node-app/any/nested/path.
Root if a Node.js application is __dirname
, not process.cwd()
ps: wait... technically there is no such thing as a root of the application. So you should clarify what do you mean by that.
Suppose you have an app called node-app in the path var/www/node-app with an entry point as index.js. Then define the following in index.js
exports.path = __dirname;
Now assuming the other app is in var/www/some-other-deeper-path/new-app, and we require to know the path at which node-app is, we have to require node-app in new-app using
var node-app = require('node-app');
console.log(node-app.path); // this will log var/www/node-app