How to get the current directory for an nw.js executable

佐手、 提交于 2019-12-12 04:21:57

问题


Struggling to find the current directory when I create an nw.js executable.

I have created a Mac server application with a mix of html and Javascript and a lot of node.js module usage. I have two xml files that the server accesses and that reside in the same folder as the application.

When I run the application as follows: ./nwjs.app/Contents/MacOS/nwjs . and find the current directory as follows: var workingFolder = process.cwd(); then my working folder is the one where the application and xml files reside.

However, if I follow the 'package and distribute' guidelines (create a .nw file, name it app.nw, and put in contents/resources), and then run the .app executable, I get the following as the working folder: /private/var/folders/3x/2hq40kzs59q8gj6yfn4ktj940000gn/T/.io.nwjs.nw.HcwnF2

I assume that the context changed. So I have been looking at alternatives to get the current directory. I tried: var workingFolder = global.__dirname; and got the same result. Then: var workingFolder = process.env.PWD; and got undefined (although this worked for the 'unpackaged' version)

How do I get the current directory?

Any ideas would be much appreciated!


回答1:


Node-Webkit extracts the *.nw file to the temporary directory so that temporary directory is the current working directory. You can use side by side packaging (Putting the package.json file and nw.exe in same folder without compressing) method to keep the working directory to the folder where nw and package.json file resides. You can use the following to get the current path

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



回答2:


For Windows use: var path = require('path'); path.dirname(process.execPath);

For Mac and Linux use: global.__dirname;



来源:https://stackoverflow.com/questions/41533860/how-to-get-the-current-directory-for-an-nw-js-executable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!