How to append values to the PATH environment variable in NodeJS? How to append values to the PATH environment variable in NodeJS? windows windows

How to append values to the PATH environment variable in NodeJS?


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])// STDOUTvar stdOut = result.stdout.toString();console.log(stdOut)// STDERRvar 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:

  • 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


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 PATHconst current_value = process.env.PATH;console.log("PREV VALUE:")console.log(current_value)// Add the additional entryconst addl_entry = String.raw`\my\new\path\component`process.env["PATH"] = addl_entry + ";" + current_value// Display the new valueconsole.log("NEW VALUE:")console.log(process.env.PATH)