24

So I have node modules that can be require for Internationalization.

I'm trying to get the current path of the file that run my node module inside the nodule module.

Use case #1:

Inside ~/yourProject/node_modules/i18n.js

var current_path_to_locales_dir = path.join(__dirname, "locale");

And the path of the server is:

~/YourUserName/yourProject/app.js

Doing var i18n = require("i18n");

And trying to get the path it will return

/User/YourUserName/yourProject/node_modules/locale

Which is correct but I'm expecting it to look for

/User/YourUserName/yourProject/locale

Use case #2:

Inside ~/i18nProject/i18n.js

var current_path_to_locales_dir = path.join(__dirname, "locale");

If I have a sample app in the ~/i18nProject/sample and doing var i18n = require("../i18n");

The locale directory this time will be

/User/YourUserName/i18nProject/locale

Again the above is correct but I would expect it to be

/User/i18nProject/sample/locale/

Now I'm wondering if there is a way that I can get the path of the current running script?

Ali
  • 9,997
  • 20
  • 70
  • 105
  • 3
    try `process.cwd()`, is this not what you're looking for? – vmx Dec 17 '13 at 18:31
  • @vmx `process.cwd()` would return the path to the place where you run the file? – Ali Dec 17 '13 at 18:33
  • 1
    it returns the current working directory. If left untouched, that is the directory you ran `node app` or the like in, but users can use `process.chdir` and change it. If you tie into it before users change the working dir, you're good, but it's not guaranteed correct. – Mike 'Pomax' Kamermans Dec 17 '13 at 18:35

2 Answers2

20

Use the variables __filename, __dirname will return called modules/scripts name and path, see here http://nodejs.org/docs/latest/api/globals.html#globals_filename

Update

In your case you need to get the caller of module/script:

Node.js does not do it for you, so technically you may have to add extra wrapper and parameter to your module and convert it into a function that accepts this information.

However, there is a work around to implement getCaller by using Error.prepareStackTrace This thread already has your solution:

How can one get the file path of the caller function in node.js?

Community
  • 1
  • 1
Nitin...
  • 1,274
  • 10
  • 18
  • 3
    `console.log(__filename);` will return the path to the file not the path of the file that ran the script. – Ali Dec 17 '13 at 19:33
3

Case 1:

If you want a default locals directory in the project of the user consuming your npm package, change this line:

var current_path_to_locales_dir = path.join(__dirname, "locale");

to:

var current_path_to_locales_dir = path.join(__dirname, "..", "locale");
// The ".." moves back one directory out of node_modules. 

Just be sure that the file running the above code is in the root of your npm package. It might be better to change to path.resolve but I'll let you explore the differences as this is out of the scope of your question.

Case 2:

If you want to access a sample directory in your npm package by using require("../i18n");, change this line:

var current_path_to_locales_dir = path.join(__dirname, "locale");

to:

var current_path_to_locales_dir = path.join(__dirname, "sample", "locale");

Same as case 1, make sure the file running above code is in the root of your npm package

Community
  • 1
  • 1
srquinn
  • 10,134
  • 2
  • 48
  • 54
  • Well that is a bit like we are hardcoding the path no? like I don't really know when is the case they will use either one of the two. – Ali Dec 17 '13 at 19:31
  • Sorry, I inferred from you code that you were intending to force a directory structure on the consumer of your npm package. – srquinn Dec 17 '13 at 19:33
  • Yeah, not really like that unfortunately is not going to be known about the use case but I'm just giving some possible use case here :( – Ali Dec 17 '13 at 19:34
  • Setting directory locations through a configuration object is probably going to be your best bet – srquinn Dec 17 '13 at 19:35
  • which is what I have for the user right now. I thought it might be a way to do so if not I guess I have to go back to the old fashion way. Provide us the `dir` :P – Ali Dec 17 '13 at 19:36
  • 1
    Classic battle of Convention vs. Configuration. My answer will set a convention, but for maximum flexibility, you'll want to use configuration =) – srquinn Dec 17 '13 at 19:37