9

Following the answer suggested in the question -

Is it possible to permanently set environment variables?

I was able to set new environment variables permanently with the command -

spawnSync('setx', ['-m', 'MyDownloads', 'H:\\temp\\downloads'])

But now my goal is to append new values to the PATH environment variable.

Is it possible?

Paz
  • 1,023
  • 4
  • 14
  • 31

3 Answers3

3

Why don't you just get the environment variable and then append to it?

I.e.

const {spawnSync} = require("child_process");
const current_value = process.env.PATH;
const new_path_value = current_value.concat(";", "/some/new/path");

var result = spawnSync('setx', ['-m', 'PATH', new_path_value])

// STDOUT
var stdOut = result.stdout.toString();
console.log(stdOut)

// STDERR
var stdErr =  result.stderr.toString();

if(stdErr === '') {
    console.log('Successfully set environment variable')
} else {
    console.log(`ERROR: ${stderr}`)
}

Update "/some/new/path" and run this as admin as the link you provided suggests and it should work.

marksy_91
  • 196
  • 1
  • 3
  • Are you able to source the current environment variable from process.env? What happens when you console.log(current_value) and console.log(new_value)? – marksy_91 Jan 14 '20 at 23:21
  • It was my bad, your solution was correct! thank you very much – Paz Jan 16 '20 at 12:19
  • I needed to change the line ```console.log(`ERROR: ${stderr}`)``` to ```console.log(`ERROR: ${stdErr}`)``` – mherzog Apr 25 '21 at 13:30
2

Run your script with the admin permission:

  • Open cmd or PowerShell with admin
  • Run node your_script.js
  • To append PATH variable, you can set value is : %PATH%;your_new_value here (%PATH% get old value)

If you run with electron app, you should require admin permission.

Don't forget setx run on window

enter image description here

hong4rc
  • 3,999
  • 4
  • 21
  • 40
  • What is the difference between appending to an old value and set a new value? – Paz Jan 14 '20 at 21:47
  • @Paz You can add `%PATH%` to get old value and append your new value – hong4rc Jan 15 '20 at 01:56
  • Ok, now I understand. That's was the first thing that I did with Electron. I know that the command in windows is setx -m '%path%;NEW_VALUE' but doesn't work with Electron – Paz Jan 15 '20 at 09:35
  • @Paz do you run it with admin permission? – hong4rc Jan 15 '20 at 09:39
  • of course. the admin permission is only required for the "-m", meaning the variable will be system variable and not user variable. that's the easy part. I'm always ending up setting a new value that overrides the old one instead of appending a new value. – Paz Jan 15 '20 at 14:12
0

I don't have rights to modify my registry, and I also would rather not call an OS command such as setx.

The following adds an additional component to the Windows PATH. I then ran Selenium, which uses the new setting.

// Display current value of PATH
const current_value = process.env.PATH;
console.log("PREV VALUE:")
console.log(current_value)

// Add the additional entry
const addl_entry = String.raw`\my\new\path\component`
process.env["PATH"] = addl_entry + ";" + current_value

// Display the new value
console.log("NEW VALUE:")
console.log(process.env.PATH)
mherzog
  • 1,085
  • 1
  • 12
  • 24