'How to get program's current directory in Node.js program using pkg?

I use node.js and i install pkg module to convert my js file to exe.

pkg app.js

But after convert, __dirname variable is invalid. It does not point it's(my program's) full directory path. It just get very strange path like 'C:/snapshot/NodeProj'. That path is not exist and before packaging __dirname variable was fine but it just get invalid path after packaging.

How should i gonna do to get real directory path?



Solution 1:[1]

you can use process.pkg property as conditional variable.

it'll be undefined during development mode, that time you can use __dirname

and during packaged application it'll have value so you can use process.cwd()

Solution 2:[2]

In case anyone wants a ready solution, here you go:

const path = (process.pkg) ? process.cwd() : __dirname;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Excalibur
Solution 2 crazy_enter