Get the path to the current application in node-webkit
The accepted answer's link is no longer available, so here is a short answer:
nw.js extract the content of your app, to a temp directory, every time you run it.
If you want to access the path where nw.js extracted your app, use process.cwd()
In other causes, where you want to access the path of your executable app, use:
var path = require('path');
var nwDir = path.dirname(process.execPath);
The answer to this question was discussed here: https://groups.google.com/d/topic/node-webkit/IwGzluFC9iU/discussion
On Windows, use "process.execPath" to see the path of the executable that launched it. Then work from there, removing the executable's filename from the path to get the folder's path (assuming your app's .nw is relative to the executable or is combined with it).
This works for me whether it is running with the zipped 'app.nw' or where 'nw.exe' and 'app.nw' are combined into one executable file (app.exe).
If you are looking for the path to the App source (i.e. the folder that contains package.json
) then you can use process.cwd()
.
No matter what the environment's true working directory is when the node executable is launched, it will set process.cwd()
to the location of the App source. If the App is contained in an archive, cwd will point to the temporary folder where the source is extracted.
Importantly, note that process.cwd()
can be changed during the application run by process.chdir(newPath)
and potentially by other events as well, so you might want to store the initial value at application launch.
EDIT:
Just to clarify, process.cwd()
is set to the folder that contains the actual package.json
file that is used by the running app. So if you have packaged your app in an archive or executable (zip, exe, nwz, nw, etc), then nw.exe will extract the project files to a temporary directory before running the app. So process.cwd()
will point to that temporary folder, not the location of the original archive or executable.