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?
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.
Run your script with the admin permission:
node your_script.js
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With