Process.env on Mac returns incomplete information

3 views
Skip to first unread message

Jehad Affoneh

unread,
May 9, 2013, 7:36:11 PM5/9/13
to node-...@googlegroups.com
If I run node on my machine and run process.env I am able to see the environment variables I've defined in .profile or bash_profile but when running node-webkit, I only get the system defined variables (like HOME, USER, etc) and I do not get any of the variables I defined in .profile or .bash_profile.

Jehad Affoneh

unread,
May 21, 2013, 10:51:25 AM5/21/13
to node-...@googlegroups.com
Any updates on this one? Anyone running into this issue?

para...@gmail.com

unread,
Jul 20, 2013, 7:15:12 AM7/20/13
to node-...@googlegroups.com
Hi Jehad,

node-webkit only inherits environment variables from the bash profile if it is run from the shell. On OSX:

$ open -n -a node-webkit ~/nw-test/

... Works great!

If you want to pull in all environment variables from the user's bash profile when launched from a GUI, you'll have to run the default shell in a child process and parse the environment:

function get_env(callback) {
    var sh = require("child_process").spawn("sh", [
        "-c",
        "source ~/.bash_profile; env"
    ]);
    var result = "";
    sh.stdout.on("data", function (data) {
        result += data.toString();
    });
    sh.on("close", function () {
        var env = {};
        var lines = result.split("\n");
        for (var i = 0; i < lines.length; i++) {
            var kv = lines[i].split("=", 2);
            env[kv[0]] = kv[1];
        }
        callback(env);
    });
}

get_env(function (env) {
    document.write(JSON.stringify(env));
});
Reply all
Reply to author
Forward
0 new messages