Kyle Slattery Kyle Slattery - 2 years ago 134
Node.js Question

How to get path to current script with node.js?

How would I get the path to the script in node.js? I know there's

, but that only refers to the directory where the script was called, not of the script itself. For instance, say I'm in
and I run the following command:

node /home/kyle/some/dir/file.js

If I call
, I get
, not
. Is there a way to get that directory?

Answer Source

Found it after looking through the documentation again. What I was looking for are the __filename and __dirname module-level variables.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download