Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the path to the current application in node-webkit

Tags:

node-webkit

In node-webkit, is there any way 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 variable __dirname appears to be undefined.

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

console.log(__dirname)

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

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

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

like image 333
Anderson Green Avatar asked Dec 09 '12 00:12

Anderson Green


Video Answer


2 Answers

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);
like image 166
Eric Saboia Avatar answered Sep 24 '22 00:09

Eric Saboia


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).

like image 38
Roger Wang Avatar answered Sep 25 '22 00:09

Roger Wang