Get app root path

6,078 views
Skip to first unread message

Alan Hoffmeister

unread,
May 26, 2012, 9:13:10 PM5/26/12
to nodejs
Hello there!

How can I get the main path of the app?
Take a look at this example:

1) I have a module that return the app path, and I install it globally
    npm install -g mymodule

2) Than I have my app.js installed on /home/myuser/app.js

   mymodule = require('mymodule');
   console.log(mymodule.path());

3) mymodule.path() needs to return the app path: /home/myuser

How can I do this?

--
Att,
Alan Hoffmeister

mscdex

unread,
May 26, 2012, 9:20:09 PM5/26/12
to nodejs
On May 26, 9:13 pm, Alan Hoffmeister <alanhoffmeis...@gmail.com>
wrote:
> How can I get the main path of the app?

// module
var path = require('path');

exports.path = function() {
return path.dirname(module.parent.filename);
};

Alan Hoffmeister

unread,
May 26, 2012, 9:24:43 PM5/26/12
to nod...@googlegroups.com
@mscdex Thanks, but what if the parent is inside a folder on the main path? It would return /home/myuser/somefolder instead of /home/mysuser.

--
Att,
Alan Hoffmeister


2012/5/26 mscdex <msc...@gmail.com>

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Alan Hoffmeister

unread,
May 26, 2012, 9:38:06 PM5/26/12
to nod...@googlegroups.com
I think that I got the answer:

//mymodyle.js
path = require('path');
exports.path = path.dirname(process.mainModule.filename);

Can I trust on the information passed by process.mainModule.filename?

--
Att,
Alan Hoffmeister


2012/5/26 Alan Hoffmeister <alanhof...@gmail.com>

Anand George

unread,
May 26, 2012, 10:17:05 PM5/26/12
to nod...@googlegroups.com
If you need to find the app path couldn't you just use __dirname.

Alan Hoffmeister

unread,
May 26, 2012, 10:23:00 PM5/26/12
to nod...@googlegroups.com
__dirname return the path of the script that is calling it, not the app root path...
--
Att,
Alan Hoffmeister


2012/5/26 Anand George <mranan...@gmail.com>

Anand George

unread,
May 26, 2012, 10:29:34 PM5/26/12
to nod...@googlegroups.com
Not sure if I understood your question right.

console.log(__dirname) in app.js which is in the path /home/myuser/app.js will return /home/myuser.

Isn't this what you intend.

Alan Hoffmeister

unread,
May 26, 2012, 10:37:47 PM5/26/12
to nod...@googlegroups.com
Yep, you understood, but what if i need to know the root path from a required() module that is somewhere eles on the system?

Anand George

unread,
May 26, 2012, 11:33:03 PM5/26/12
to nod...@googlegroups.com
It still works. This is what I have done. Created two folders

/home/anand/testing/dirpath - the npm module

/home/anand/testing/pathtest - test folder

dirpath has the following files

index.js

exports.path = __dirname;

and

package.json

{
  "name": "dirpath",
  "version": "0.0.1",
  "private": true,
"main": "index.js",
  "dependencies": {
  },
  "engines": {
    "node": "v0.6.x"
  }
}

run npm link in dirpath so that the module is available globally

pathtest has one file app.js

var dirpath = require('dirpath');

console.log(dirpath.path);

Make dirpath module available in current directory using npm link dirpath

Next run app.js and the console logs

/home/anand/node/Testing/dirpath

mscdex

unread,
May 27, 2012, 1:40:40 AM5/27/12
to nodejs
On May 26, 11:33 pm, Anand George <mranandgeo...@gmail.com> wrote:
> Next run app.js and the console logs
>
> /home/anand/node/Testing/dirpath

I think he wants the path of app.js from the module, so: '/home/anand/
node/Testing/pathtest' is what he is expecting.

Anand George

unread,
May 27, 2012, 3:03:10 AM5/27/12
to nod...@googlegroups.com
Ok. I get it now. And thanks for both solutions offered. Guess it's useful in cases like the socket.io implementation where you wish to link some client-side libraries in the path.

Alan Hoffmeister

unread,
May 27, 2012, 7:44:26 AM5/27/12
to nod...@googlegroups.com
@mscdex that's it.

I need this to parse config files, and now I can require() my parser from everywhere without the need to send the app path :)

--
Att,
Alan Hoffmeister


2012/5/27 Anand George <mranan...@gmail.com>

Anand George

unread,
May 27, 2012, 9:12:55 AM5/27/12
to nod...@googlegroups.com
Coming back to your question

npm install -g mymodule will not be available when you require it using 

mymodule = require('mymodule')

Alan Hoffmeister

unread,
May 27, 2012, 9:48:26 AM5/27/12
to nod...@googlegroups.com
@Anand, yep, but you can use `npm link mymodule`

Anand George

unread,
May 27, 2012, 10:39:38 AM5/27/12
to nod...@googlegroups.com
Hope you don't mind my asking... but do you really need a parser for a config file. Couldn't  it just be required as below:

config.js

module.exports = {
    host: "localhost",
    port: 8000
}

app.js

var c = require('./config')
console.log(c.host + c.port);

Alan Hoffmeister

unread,
May 27, 2012, 12:00:35 PM5/27/12
to nod...@googlegroups.com
Yup, I could do this, but I'm writting a MVC framework and splitting routes, views, rpc, models, configs, validations, translations an other things between multiple folders, that's because I really don't like to have my huge app inside one big js.

My main idea is to maintain a simple module that could load this parts from everywhere, I mean that I can use:

var mvc = require('mvc');
users = mvc.model('User');
conf = mvc.conf();

Inside myapp/controller/index.js, inside myapp/rpc/user.js or whatever, without worrying how far I'm from my user model schema or my config files...

This way I just need to configure my MVC main module to look inside the right places and forget ugly relative paths like require('../../config/database/production') and etc.

I don't know if I could express myself the way that I intended, but that's my idea :)


--
--
Att,
Alan Hoffmeister
Message has been deleted

C. Mundi

unread,
May 27, 2012, 2:05:28 PM5/27/12
to nod...@googlegroups.com

Amen.  I went through the same thing.  I wish this had been a higher design priority for node, but I think I see why it wasn't.  Moving JavaScript out of the browser and onto the server comes with some challenges.

Alan Hoffmeister

unread,
May 27, 2012, 2:39:31 PM5/27/12
to nod...@googlegroups.com
@Shogun very nice! I thought global was some evil thing..
@Mundi, yes, I came from PHP and I really miss this folder pattern...


--
--
Att,
Alan Hoffmeister

Nathan Quinn

unread,
Sep 2, 2015, 2:43:33 PM9/2/15
to nodejs
process.cwd();

Sávio Lucena

unread,
Sep 3, 2015, 1:42:22 PM9/3/15
to nod...@googlegroups.com, nodejs
Alan,

I don’t know if that previews answer gave you what you were looking for but anyway just adding to it:

From Azat Mardan:

4.7 __dirname vs. process.cwd


__dirname is an absolute path to the file in which this global variable was called, while process.cwd

is an absolute path to the process that runs this script. The latter might not be the same as the former

if we started the program from a different folder, e.g., $ node ./code/program.js.


Sávio Lucena


You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/d7a3b9d1-251b-490c-a487-88e7f1c2ae57%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages