Node.js: "__dirname" Pointing to a Temporary Folder

Why do you think that? pwd prints the current working directory, which is just the $PWD environment variable I believe.

According to what you posted from the documentation, __dirname is an environment variable that tells you the absolute path of the directory containing the currently executing file.

So if in the shell you do:

cd blah
./node.js

then they will be the same, but if you do

cd blah
/whatever/whatever/node.js

then they would be different.

Terminal will normally reopen in to the same directory as it was last in. It generally starts in the home directory by default, but for example, if you make a new window, cd to a location, quit and relaunch, then it will reopen the window into that same location.

In any event, when you execute a shell script from Keyboard Maestro, the CWD will be "/" and the node.js __dirname will be the parent folder of the file, either the file you specify, or a temporary folder Keyboard Maestro uses.

There is no way to set the CWD with the Execute Shell Script action (note the ENV_PWD Keyboard Maestro variable does not work because the system NSTask does not honor that, it sets it explicitly - I may be able to change that in a future version).

In any event, if you want the CWD to be something in your script, you will need to set it explicitly - the __dirname has nothing to do with the CWD - that they are the same when you execute a script in the current working directory is a coincidence only.

1 Like