Get path to current application in node -webkit

Is there a way in node-webkit to find the path to the current application? In node.js you can use __dirname to find the path to the current application, but in node -webkit the __dirname variable looks like undefined.

The following node.js script correctly prints the file path:

console.log(__dirname)

The following node -webkit script does not correctly print the file path:

 <script type = "text/javascript"> alert(__dirname); </script> 

What is the correct way to find the path to the current application in node-webkit?

+8
node-webkit
source share
5 answers

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 to the executable that ran it. Then work from there by removing the executable file name from the path to get the path to the folder (assuming that your .nw application belongs to the executable file or is combined with it).

This works for me, whether it works with zipped 'app.nw' or where 'nw.exe' and 'app.nw' are combined into one executable file (app.exe).

+5
source share

The accepted return link is no longer available, so here is a short answer:

nw.js extracts the contents of your application into the temp directory every time you run it.

If you want to access the path where nw.js extracted your application, use process.cwd()

In other cases, when you want to access the path to an executable application, use:

 var path = require('path'); var nwDir = path.dirname(process.execPath); 
+5
source share

If you are looking for the path to the application source (i.e. the folder containing package.json ), you can use process.cwd() .

Regardless of which working directory the environment works when the node executable is running, it will install process.cwd() at the location of the application source. If the application is in the archive, cwd will point to a temporary folder where the source will be extracted.

It is important to note that process.cwd() can be changed during the start of the process.chdir(newPath) application and potentially other events, so you may want to keep the initial value when the application starts.

EDIT: To clarify, process.cwd() installed in the folder containing the actual package.json file that is used by the running application. Therefore, if you packed your application in an archive or executable file (zip, exe, nwz, nw, etc.), then before starting the application, nw.exe will extract the project files to a temporary directory. Thus, process.cwd() will point to this temporary folder not the location of the source archive or executable file.

+2
source share

this should work:

 var nw = require('nw.gui'); //This line is only required for NW.js 0.12.x and below console.log(nw.__dirname) 
+1
source share

window.location will not work if you loaded the external uri, but the following seems reliable independently:

 var path = require('path'); ,appPath = path.dirname(require.main.filename) 
0
source share

All Articles