1

I'm using node@18.15.0 and pnpm@8.6.5 and sudo-prompt@9.2.1. I have the following command I want to execute node ./test.cjs, but with privileged permissions.

When I execute it without permissions, it works and I see logs. This is the file content of test.cjs script:

const sudoPrompt = require('sudo-prompt');

sudoPrompt.exec('node ./scripts/spawn-process.cjs', (error) => {
   if (error) {
      throw error;
   }
});

then, the scripts/spawn-process.cjs file content:

const { spawn } = require('node:child_process');

const spawner = spawn('webpack', ['--config', './webpack/webpack.config.dev.ts'], {
    windowsHide: true,
    shell: true,
    stdio: 'pipe',
});

process.stdin.pipe(spawner.stdin);

spawner.stdout?.on('data', (data) => {
    console.log(data);
});

spawner.stderr?.on('data', (data) => {
    console.log(data);
});

spawner.on('close', (exitCode) => {
    process.exit(exitCode);
});

Everything is working, the command is spawned successfully and running as required, but I don't see any output in the terminal from the console.log. When I run the webpack command as npm script: "test": "webpack --config ./webpack/webpack.config.dev.ts", and then running sudo pnpm test (for example in macOS), I do see output in the console.

Tal Rofe
  • 457
  • 12
  • 46
  • Does this answer your question: https://stackoverflow.com/a/36937488/3930247? – technophyle Aug 14 '23 at 07:25
  • @technophyle No, this is exactly what I've done – Tal Rofe Aug 14 '23 at 07:28
  • the callback in `sudoPrompt.exec` also takes a `stdout` and `stderr` . So probably those are captured during execution and passed to the callback once the execution is finished ... – derpirscher Aug 14 '23 at 08:29
  • @derpirscher Maybe, but it does not help here. I need to log it in real time to the console and this is why I run `spawn`. The command won't be executed within few seconds. It can take infinity time – Tal Rofe Aug 14 '23 at 11:25

0 Answers0