Frage

I've got a simple program that can be run from command line. This program is for live preview of markdown files. When i'm packaging the app with

cat $NW/nw marknow.nw > marknow

and running from different location:

./build/marknow ../relative/path/to/file.md

I can't get current working directory.

process.cwd()

is returning /tmp/something???

How can I get working directory in node-webkit? Directory from which ./build/marknow ../relative/path/to/file.md was called.

War es hilfreich?

Lösung 2

I think cwd is getting 2 different meanings:

  1. directory where the app lives
  2. directory where the user launch the app from

In shell scripting I can see the 2nd meaning applied, but the node-webkit community seems to be using the 1st

Correct me if I'm wrong, but I think you are looking for the 2nd: the path where the user was when calling to your app.

After some tests I finally found a way that works for me:

process.env.PWD

Andere Tipps

Another option you could try if the cwd doesn't seem to work is getting the execution directory with something like this:

var path = require('path');
var execPath = path.dirname( process.execPath );

This should get you the execution directory of the exe. cwd gets the temp directory because of how node-webkit handles opening the files from a temp directory on each run.

As is pointed out in other answers, node-webkit starts up the node process with the wrong 'current working directory' (from a command line perspective anyway).

As is pointed out elsewhere, process.env.PWD in fact contains what we want (when the app is started from the command line). A simple workaround could then be to do the following in your index.html (or some other place that gets executed early):

<script type="text/javascript">
  if(process.env.PWD) {
    process.chdir(process.env.PWD);
  }
</script>

Presto! Now process.cwd() returns the right thing.

Apparently, this trick only really works for apps that are started from the command line - but then again, it is probably only necessary when the app is launched from the command line.

There is a ticket for this issue, but it is old and dusty

If all answer were failed, try this.

  1. Create file "main.js" and copy this code.

    // get child_process module.
    var childproc = require('child_process');
    
    // start process using child_process
    //  with current path string
    // '__dirname' would be not only path
    //  of 'main.js' but also one of 'app.zip'
    //  because 'main.js' and 'app.zip' have same directory
    childproc.exec('nw app.zip ' + __dirname);
    
  2. Add this code which you want to get current path

    // get 'nw.gui' module
    var gui = require('nw.gui');
    
    // get arguments that we passed
    var arr = gui.App.argv;
    
    // nw app.zip __dirname
    //            ^ argv[0]
    alert('current path: [' + arr[0] + ']');
    
  3. Run "main.js" with node.js

I posted "How to get current directory(current path) using node-webkit".

http://hdnua.tistory.com/16

It gets executing zip file's directory.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top