Dan P
Dan P

Reputation: 924

How to get the directory of the .exe for a Node.js pkg executable?

I'm compiling/building a Node.js project into an .exe using Vercel's pkg but I'm trying to find where that executable file is during the program's runtime. To clarify, I want the Node.js program to know where the .exe file it is being run from is, to confirm it is correctly installed.

Context: I cannot guarantee where the user has the .exe stored the first time they run it, but I want it to copy itself to the AppData folder so I can setup a Windows service from it, if it's not being run from there already. This in hopes of making a self-installing program, that doesn't require me to build an installed around it.

So on it's first run, I need the location of the .exe to be able to copy where I want. I might just have not picked it up, but I can't find an answer on either the pkg documentation or on another question here.

What I've Tried: I have tried process.cwd, process.pkg, __filename, __dirname and so on, but they all lead to the wrong thing, usually the snapshot folder of files.

Upvotes: 1

Views: 2529

Answers (1)

Meddah Abdallah
Meddah Abdallah

Reputation: 706

When it comes to the main script it's as simple as finding it:

process.argv

From the Node.js documentation

Pkg from Vercel, is not an installer, its just a packager that will package node binaries and your code in a single exe.

You can follow this tutorial that shows how to make an installer it even enables you to set a path of where you want your exe to be installed

Upvotes: 4

Related Questions