How to append values to the PATH environment variable in NodeJS?
Asked Answered
A

3

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?

Attrahent answered 2/1, 2020 at 7:57 Comment(4)
This seems like a question about Windows and the Setx command, not Node, JavaScript, or Electron.Hiatus
You are right! Ill fix itAttrahent
Are you try to use my answer?Viperous
I didn't understand your answer. admin permission is not the problem in my case.Attrahent
U
5

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.

Unhinge answered 13/1, 2020 at 6:12 Comment(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)?Unhinge
It was my bad, your solution was correct! thank you very muchAttrahent
I needed to change the line console.log(`ERROR: ${stderr}`) to console.log(`ERROR: ${stdErr}`)Immigration
V
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

Viperous answered 3/1, 2020 at 10:32 Comment(5)
What is the difference between appending to an old value and set a new value?Attrahent
@Attrahent You can add %PATH% to get old value and append your new valueViperous
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 ElectronAttrahent
@Attrahent do you run it with admin permission?Viperous
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.Attrahent
I
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)
Immigration answered 25/4, 2021 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.