How to get working directory in node -webkit

I have a simple program that can be run from the command line. This program is designed to preview markup files. When I pack the application with

cat $NW/nw marknow.nw > marknow 

and executed from another place:

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

I can not get the current working directory.

 process.cwd() 

returns / tmp / something ???

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

+6
source share
4 answers

I think cwd gets 2 different values:

  • where is the application
  • where the user launches the application from

In shell scripts, I see that the second value applies , but the node -webkit community seems to use the first

Correct me if I am mistaken, but I think that you are looking for the second: the path that the user used when calling to your application.

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

process.env.PWD

+6
source

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

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

This should provide you with an exe runtime directory. cwd gets a temporary directory due to the way node -webkit handles opening files from the temp directory every time it starts.

+13
source

As pointed out in other answers, node -webkit starts the node process with the wrong "current working directory" (in any case, from the point of view of the command line).

As stated elsewhere, process.env.PWD actually contains what we want (when the application starts from the command line). A simple workaround could be to do the following in your index.html (or elsewhere that is done earlier):

 <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 really only works for applications running from the command line, but again, it is probably only necessary when the application is launched from the command line.

There is a ticket for this problem, but old and dusty

+1
source

If the answer was unsuccessful, try this.

  • Create the 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); 
  • Add this code that you want to receive in the 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] + ']'); 
  • Run "main.js" with node.js

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

http://hdnua.tistory.com/16

It gets the executable directory of the zip file.

+1
source

Source: https://habr.com/ru/post/945727/


All Articles