0
var jf = require('jsonfile');

jf.writeFile("./setting.json", newSettings, function(err) {
    if (err){
        console.log("writing file err: ", err);
    } else {
        console.log("setting saved successfully.");
    }
});

Above code reports successfully in both development (npm start) and distribution (zip to app.nw and put it in node-webkit.app Resources dir), but only the file in development changed. I dont know why. Have tried utf8 and base64 encoding, both of them are the same.

Or is there a way to get around it? Like put the setting.json file somewhere else?

Russj
  • 697
  • 1
  • 10
  • 22
  • I tried to use PouchDB. The same thing happened. It works in development, but once I put app.nw in the resource, the changes can not be saved. – Russj Jan 15 '15 at 06:44
  • Do you really need a module for JSON write file? Couldn't you just: `fs.writeFile("./settings.json", JSON.stringify(settings, null, 4), function(err){})`? It will output a formated JSON file. – Daniel Cheung Jul 04 '15 at 04:25
  • Have you checked your program has the permission to access the target path? – Daniel Cheung Jul 04 '15 at 04:29

1 Answers1

0

The file you are looking for is in the local temp folder. For example: "C:/Users/[YourUserName]/AppData/Local/Temp/[node_webkit_uniqueid]"/. Your setting json file should be inside /Local/Temp/ folder.

To obtain the execution directory of the production environment. Try:

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

To differentiate between production and development environment, you can set the environment variable in your package.json file.

"scripts": {
   "start": "set NODE_ENV=development && nw"
}

And in your code, you can retrieve the environment variable like this.

var env = process.env.NODE_ENV || 'production';
if (env === 'production') {
   //production settings
} else {
   //development settings
}
EuWern
  • 123
  • 7