How to get current path of the file that run the script in nodejs

Viewed 48275

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?

2 Answers
Related