4
votes

I have a function that should open a directory after it was created,

setTimeout(function()
{
    var fs = require('fs');
    console.log(newPath);
    var open = fs.opensync(newPath, 'r');
}, 2500);

But this doesn't seem to work. I am getting the following errors

first is,

TypeError: undefined is not a function at eval (eval at <anonymous> (file:///Users/proslav/Library/Developer/Xcode/DerivedData/trackingCore-ecxfviftqracjxhimcuhhhvyddso/Build/Products/Debug/trackingCore.app/Contents/Resources/timeBroFront.app/Contents/Resources/app.nw/js/jquery-1.10.2.min.js:3:4994), :43:18)

and second is,

Uncaught ReferenceError: require is not defined

I was thinking that it could be that my variable newpath is undefinded but the log shows me the right link. The creation of the directory with var fs = require('fs'); works fine.

What am I doing wrong here?

2
Looks lime you have this in a client side JS file, you must have it in a server side file. - Shadow Wizard Hates Omicron
I'm stuck here a little bit. How would a server side JS-File look like in Node.js using node-webkit? - Silve2611
Look in the linked dupe answer, it should solve your case too. - Shadow Wizard Hates Omicron
Actually i found the right answer. Node-webkit has a function for it. I cannot post it because to question is marked as duplicate - Silve2611
hmm.. OK, you can now post it. - Shadow Wizard Hates Omicron

2 Answers

5
votes

I found out how it has to be done. Node-webkit offers a function for that. It is working on MAC and should also work on windows. The function below is an example function. nw.gui and gui.Shell.showItemInFolder did the thing for me. Thx for the input.

/*---------
Open Folder
---------*/
function openFolder(path){
    var gui = require('nw.gui');
    gui.Shell.showItemInFolder(path);
}
1
votes

In nw.js version 0.13 or later, use:

nw.Shell.showItemInFolder(fullpath);

Version < 0.13:

var gui = require('nw.gui');
gui.Shell.showItemInFolder(fullpath);

Note that the full path name is required. If it doesn't exist, it will fail silently.

If the path is something like c:\foo\bar.txt, it will open the folder foo and highlight the file bar.txt.

If the path is c:\foo\foo2, it will open the folder foo and highlight the folder foo2 (I expected it to open the folder foo2, but it will open the parent).


To find the fullpath of the running app, as we can't use node functions in our front-end (that's why you had the error trying to load the fs module), I've created a node module (utils.js) with the following:

exports.getFullPath = function(fileName) {
    var path = require('path');
    return path.resolve(__dirname, fileName);
}

In the front-end:

function openFolder(path) {
    var utils = require('./utils');
    var fullpath = utils.getFullPath(path);
    nw.Shell.showItemInFolder(fullpath);
}