Skip to content Skip to sidebar Skip to footer

Requiring/running Javascript Outside Of Node-webkit Application

Suppose I have the following app structure: outer-folder/ ├── my-app/ └── settings.js where my-app/ is either the unbuilt directory that contains package.json or the p

Solution 1:

I suggest you to use the application data path. See the documentation here

An exemple

var gui = require('nw.gui');
var path = require('path');
var yaml = require('js-yaml');
var fs = require('fs');

var confPath = path.join(gui.App.dataPath, 'conf', "dev-conf.yml");
try {
  conf = yaml.load(fs.readFileSync(confPath, 'utf-8'));
} catch (err) {
  thrownewError("Cannot read or parse configuration file '"+confPath+"': "+err);
}

It's a good pratice to separate code and configuration, and App.dataPath aims at the application specific folder in user's application data (different for each OS).

I generally use an installer to copy my configuration file into it.

Next tip: I prefer using YAML instead of JSON for my configuration or settings, because it allows you to insert comments inside the file.

Post a Comment for "Requiring/running Javascript Outside Of Node-webkit Application"