1

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.

Stephen Simpson
  • 1,381
  • 1
  • 11
  • 23
  • Actually, I've found a solution to the hack from this answer (http://stackoverflow.com/questions/889518/windows-batch-files-how-to-set-a-variable-with-the-result-of-a-command). I've managed to get the following working: for /f "delims=" %%a in ('node myPathSettingScript.js') do @set PATH=%%a. However, it would be nice if the whole operation could be done in node. – Stephen Simpson Sep 17 '14 at 12:49
  • 1
    Each time a process is created, the environment block is copied from parent to child (as is or it can be customized) and changes in childs are not reflected into the parent. Each process have its own **copy** of the environment variables. Any change in environment block is only visible in the process that makes the change and in the processes started from it once the change has been made. If a change must affect a console and the processes started from it, the change must be done at the console level. node is a separated process with its own copy of the environment. You need the batch file – MC ND Sep 17 '14 at 14:17
  • 1
    Or, you can make the change in node and start anything from node. That way all the started processes will inherit this environment. – MC ND Sep 17 '14 at 14:18
  • @MC-ND Yea, I was expecting this to be the answer. Normal security might preclude the passing of environment changes back up to the parent process. However, I was hoping there was a way around this. The batch solution seems to be the best one – Stephen Simpson Sep 17 '14 at 15:06

0 Answers0