node.jswindowschild-processregedit

Trying to execute command from Nodejs (exec) throw error but works in terminal


I'm trying to run this script

let { exec } = require("child_process");

let command =
  "reg add 'HKCU\\SOFTWARE\\MySoftware' /v 'LOADER' /t REG_SZ /d 'C:\\Program Files\\MyCompany\\some.dll' /f";

exec(command, (err, stdout, stderr) => {
  if (err) console.log(err);
});

and always get the error ERROR: Invalid key name. I was thinking that maybe the reg command was incorrect but if I run it in the terminal it works.

I'm not sure what can be incorrect, I really appreciate it if you give me some feedback. Thanks


Solution

  • The solution was to replace ' with " for values

    let { exec } = require("child_process");
    
    let command =
      'reg add "HKCU\\SOFTWARE\\MySoftware" /v "LOADER" /t REG_SZ /d "C:\\Program Files\\MyCompany\\some.dll" /f';
    
    exec(command, (err, stdout, stderr) => {
      if (err) console.log(err);
    });