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

I have a very simple code to print the current working directory. I thought it is the HOME directory, it, however, appears to point to a temporary folder.



I installed Node.js from the file downloaded from the official download page.

This is very strange. Does anyone have a clue?


@peternlewis, do you have an answer for the question?
I thought the current direction is the root directory.

I don't know what __dirname prints.

Text scripts are created as temporary files, and those temporary files are executed via the shell. Keyboard Maestro does not set any current working directory, and indeed if you use pwd in a shell script it comes up as /.


Keyboard Maestro does not have any concept of a current working directory, so a script should not make any assumptions about it.

Maybe this explains:

__dirname is an environment variable that tells you the absolute path of the directory containing the currently executing file.
Node.js __dirname Variable - GeeksforGeeks

It appears node.js creates the temporary file in /private/var/folders/vl/x6z6g6y54gvg60cxn85jgnj80000gn/T and then executes it?

I'm confused by the difference between the pwd in Shell and the __dirname in node.js.
This will also show the root directory:

The process.cwd() method is an inbuilt application programming interface of the process module which is used to get the current working directory of the node.js process.
Node process.cwd() Method - GeeksforGeeks

I thought __dirname would point to the same root directory.

If I use the following, I'll also get the root directory:

The path.resolve() method is used to resolve a sequence of path-segments to an absolute path. It works by processing the sequence of paths from right to left, prepending each of the paths until the absolute path is created. The resulting path is normalized and trailing slashes are removed as required.
If no path segments are given as parameters, then the absolute path of the current working directory is used.
Node path.resolve() Method - GeeksforGeeks

Anyway, if nothing can be done, that's fine. I'll create a file in folder and execute it. Then __dirname will show the expected the directory of the file.

No, Keyboard Maestro creates the script in the temporary folder and then asks the shell to execute it. Which then sees to #! at the front and executes node with the script file.

__dirname is apparently the path to the folder that contains the script file. It is basically useless for this purpose, but in other situations (eg where you have other resources in the same folder with the script) it would be useful.

No, it is simply the parent folder of the script file, which in this case is a temporary file that Keyboard Maestro created to contain the script you wrote.

It's not that “nothing can be done”, its that it is behaving exactly as it should.

The better question is, what do you want to use __dirname for? In your original Execute Script action, there is no folder reference anywhere in the action or the script, so there is nothing that would make sense for referencing other files.

If you want to reference other files, you would need to specify the folder somehow, probably in the script, or by moving the text from the Execute Script action in to a file in a folder somewhere, and then specifying that file in the action.

But somehow you have to specify the folder.

Thanks again, Peter.
My confusion lies in the difference of the result between the pwd in shell and console.log(__dirname) in node.js. Both appear to print the current working directory. I can understand that KM creates a temporary file in /private/var/folders/vl/x6z6g6y54gvg60cxn85jgnj80000gn/T and executes it, resulting the result of console.log(__dirname). However, why then does Shell not create a temporary file in the same temporary folder, resulting the pwd command printing the same directory? It's more understandable if both print the same directory.
My knowledge is very limited. Please forgive my ignorance. I'm just trying to have a better understanding of it.

When I said that, I was thinking of the command line in When I do pwd in By default, it outputs my user home directory. So I thought something may be done to make the shell script action in KM by default outputs the user home directory with pwd in Shell (and console.log(__dirname) with node.js).

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

then they will be the same, but if you do

cd blah

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

Thank you very much! I finally got it. I was confused with "working directory" and "file directory". Once the distinction was made clear, I could fully understand.

Terminal allows customization:


I have made the next major version explicitly honour the PWD configured in ENV_PWD. If you set this Keyboard Maestro variable to a directory, Keyboard Maestro will configure the current working directory for the script to match the specified directory.

1 Like

This is great!
Thanks, Peter.