How to get current directory(current path) using node-webkit

HandyPost means document written by DoYeong Han(HDNua). Feedback is always welcome (includes syntax error. I'm not good at English).

 

1. Preface

Now I'm posting a series of document named "JSCC: Developing C Compiler Using JavaScript". So I looked for information of node.js to access user's local file system, and I found module "node-webkit" let me able to develop GUI program with HTML, CSS and JavaScript.

node.js: https://nodejs.org/

nw.js: http://nwjs.io/

I use Brackets of Adobe.

Brackets: http://brackets.io/

But it was hard to me to get current path using node-webkit. And I searched information a while and I got solution for this problem. Let me explain how to get working directory using nw module.

 

2. Project Preparation

We have to prepare project folder before get the path we want.

2.1) Make project folder. Folder's name should be 'project'.

2.2) Create 2 file and copy these codes. for nw.js application execution.

project/package.json

1
2
3
4
{
  "name""getting current path example",
  "main""index.html"
}
cs

project/index.html

1
2
3
4
5
6
7
<html>
  <body>
    <script>
      alert('hello, world!');
   </script>
  </body>
</html>
cs

This results an alert window shows 'hello, world!' string, and our goal is replacing this string to executing application's current path.

2.3) Check module node.js and node-webkit is installed.

Each program is available from official web site. node.js can be installed easily with official site's installer on Windows OS. node-webkit is provided as binary zip file. Extract it and move created folder to "C:/nwjs". So those 2 folder's full path is same with below.

C:\Users\Administrator\Desktop\project

C:\nwjs

2.4) Set nw.exe program to environment variable.

It requires two steps such below:

2.4.1) Add string "C:\nwjs;" to PATH variable.

2.4.2) Create "nw" variable and set value "C:\nwjs\nw".

When process is finished, check each case have no problems.

- "node" command must be available on command prompt.

- "project" directory should be in Desktop.

- There must be files "index.html", "package.json" in directory "project".

- There must be files extracted from "node-webkit binary" in directory "nwjs".

- "nw" command must be available on command prompt.



Preparation is end. Let's get working directory.

 

3. How to get current directory (running zip file)

node-webkit is based on node.js module. Therefore it is also possible to run node-webkit app using 'node.js' module. I thought that, it is possible to give current path string as arguments when start node-webkit process. Let me explain.

3.1) Compress project folder and rename zip file to 'app.zip'. So 'app.zip' will be in folder 'project'.

3.2) Add 'main.js' file to project folder and copy code below.

project/main.js

1
2
3
4
5
6
7
8
9
// 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);
cs

3.3) Run command prompt and change directory to it.

3.4) Enter "node main.js" and check wrote program works well.

Go through steps when program works well. If not, chekc previous steps.

3.5) Modify "index.html" as below.

index.html <html.body.script>

1
2
3
4
5
6
7
8
9
10
// 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+ ']');
 
cs

3.6) Delete previous zip file (app.zip) and re-compress project folder. Newly created zip file's name must be 'app.zip' too.

3.7) Enter "node main.js" again and see the result.


Finally, we got the current directory.

Posted by 누아니