I am hoping a node expert out there can point me in the right direction. I am trying to use a node script to change the path environment variable in Windows (Win7). The reason for this is complex but basically, I need to calculate a set of paths and then set this for the current console. I am not looking to change the global variable, just the local one used for the current console window. This would be similar to running:
Set Path = <SOME PATHS>
However, changing the path within a node script seems to affect only the running script (and not the calling console). I have tried running Set Path= using both child_process and also the more direct way of changing process.env.PATH. Neither of these carry through to the calling process.
Hence neither of the following produce the desired effect:
process.env.PATH = newPaths.join(';');
or
var exec = require('child_process').exec;
exec('PATH='+ newPaths.join(';'), function(error, stdout, stderr){
...
});
I’ve also tried hacking it by piping desired path to a batch file (which then sets the path) but I get the following error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at errnoException (net.js:901:11)
at Object.afterWrite (net.js:718:19)
To achieve this I have ended my script with
process.stdout.write(newPaths.join(';'));
and tried to pipe this to a batch file with the following content:
Set Path = %1
I think this might relate to script timings but have been unable to a pipe node script output to a batch file. I figure this ought to work but I am probably doing something wrong. However, I'd rather find a way of doing the whole operation in node if possible as the pipe method is a bit hacky.