coreybutler / node-windows

Windows support for Node.JS scripts (daemons, eventlog, UAC, etc).
Other
2.81k stars 356 forks source link

Unable to execute child process #373

Open PeterTran4 opened 6 months ago

PeterTran4 commented 6 months ago

Hi all, I've been attempting to execute this child process but after much research on both the github and Stackoverflow, still stumped.

Here is my code that is throwing an error.

const officeToPdfPath = 'C:\\\\Users\\ptran\\Desktop\\expressTesting\\OfficeToPDF.exe';
const uploadsPath = 'C:\\\\Users\\ptran\\Desktop\\expressTesting\\uploads';
let command;
let outputName =  currentDate.getTime() + "converted.pdf"
command = `${officeToPdfPath} ${uploadsPath}\\${filename} ${uploadsPath}\\${outputName}`;
return new Promise((resolve, reject) => {
        exec(command, (error, stdout, stderr) => {
          if (error) {
            console.log(error);
            reject(error);
            return;
          }
          if (stderr) {
            console.log(stderr);
            reject(new Error(stderr));
            return;
          }
          else if (stdout) {
            console.log(stdout + "GOOD PDF");
          }
          console.log("Resolve Now");
          console.log(uploadsPath, outputName)
          resolve(uploadsPath + outputName);
});

This is not the full code but seems like the issue is within the exec itself.

My error:

node:internal/process/promises:289
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error: Command failed: C:\\Users\ptran\Desktop\expressTesting\OfficeToPDF.exe C:\\Users\ptran\Desktop\expressTesting\uploads\file-1710368425793.docx C:\\Users\ptran\Desktop\expressTesting\uploads\1710368425806converted.pdf

    at ChildProcess.exithandler (node:child_process:422:12)
    at ChildProcess.emit (node:events:518:28)
    at maybeClose (node:internal/child_process:1105:16)
    at ChildProcess._handle.onexit (node:internal/child_process:305:5) {
  code: 3,
  killed: false,
  signal: null,
  cmd: 'C:\\\\Users\\ptran\\Desktop\\expressTesting\\OfficeToPDF.exe C:\\\\Users\\user\\Desktop\\expressTesting\\uploads\\file-1710368425793.docx C:\\\\Users\\ptran\\Desktop\\expressTesting\\uploads\\1710368425806converted.pdf'
}

Node.js v20.11.1

I've double checked as well to make sure that the user is an admin and running both the service as "Local System account"..."allow Service to interact with desktop" and Using a local admin account as well.

coreybutler commented 6 months ago

Is exec coming from require('child_process')? If so, that is an async function running within a promise. You either need to await/return that promise, or consider using execSync instead. Otherwise it's creating a race condition.