2

I'm writing an installation script (in node.js, specificially slush/gulp although I don't think that matters) that sets up some common tools on our developer machines.

For one of these tools, I need to modify the PATH environment variable on Windows machines.

So far the best way I've found to do this is using the winreg package to modify the Registry directly (in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path), which works great (aside from the need to run the entire install script in an elevanted command prompt).

However, it requires either a machine restart for the new PATH to take effect (not even just opening a new command prompt, as it would usually), OR sending the user into their system preferences to just open up the Environment Variables dialog box and click OK.

This detailed answer covers some of what needs to happen beneath the hood when you change an environment variable programmatically. I'm assuming the WM_SETTINGCHANGE message (details here) is sent to the system when clicking OK in that Environment Variables dialog box.

So, how could I go about sending the WM_SETTINGCHANGE message from node.js? Is that possible?

Community
  • 1
  • 1
Tim Malone
  • 3,364
  • 5
  • 37
  • 50

1 Answers1

0

I can offer non-native solution (not sure if native exists). It updated the value for me without restarts.

I'm talking about reg.exe tool that is shipped with Windows starting at least from Windows XP.

The algorithm:

1. Form a command for update, e.g.:

const scriptContent = `REG ADD HKCU\\Environment /v Path /t REG_SZ /d "${newPath}" /f`
  • HKCU\Environment - is the path to your variable in registry,
  • Path - name of the variable to update,
  • REG_SZ - type of the variable,
  • "${newPath}" - new PATH contents (fully old content with added new paths. Using quotes just in case we have white spaces there),
  • /f - force rewrite (basically, this command is for creation. So, if this variable doesn't exist, it will be created, otherwise - overwritten).

2. Write this contents to a script file, e.g. script.bat:

const fs = require("fs");
const scriptPath = 'script.bat';
fs.writeFile(scriptPath, scriptContent);

3. Execute the script file:

const child_process = require("child_process");
child_process.exec(scriptPath);
Community
  • 1
  • 1
Antenka
  • 1,519
  • 2
  • 19
  • 29