title: How to run some CMD commands on Windows 10 as administrator using Nodejs and spawn?
body: <p>I am working with Nodejs on Windows 10. Every time the app is opened I want to check if the files with the extension <code>ext</code> are associated to my app. If not I would need to run this commands on cmd in order to make the association again:</p>
  <pre><code>assoc .ext=CustomExt
  ftype CustomExt=app.exe %1 %*
  </code></pre>
  <p>But I would need administrator privileges to make this work.</p>
  <p>I have read <a href="https://groups.google.com/forum/#!topic/nodejs/UZI-25YzpUQ" rel="nofollow noreferrer"><strong>this thread</strong></a> where Jeff M says exactly what I want to achieve, but nobody answers him:</p>
  <blockquote>
  <p>From a usability perspective, <strong>the ideal</strong> would be that the user is prompted for elevated privileges only when he takes an action within my node program that actually requires those privileges. The <strong>second best option</strong> would be if the user was prompted for elevated privileges when they first start the node program. <strong>The least ideal</strong> is if the user isn't prompted at all, that it would be up to the user to always remember to start the program by right-click run as administrator. This is the least ideal because it puts to the burden on the user to remember to always start the program in a non-typical way.</p>
  <p>So, to get back to my original question, right now if I try to spawn a child process where the program being spawned requires elevated privileges (example code in my original message), <strong>the current behavior is that the user is not prompted for those privileges, and instead the node program crashes with an error.</strong> How can I get my node program to request those privileges instead of just dying?</p>
  </blockquote>
  <p>My nodejs script:</p>
  <pre><code>const { spawn } = require('child_process');
  
  const assoc = spawn(                    // how to run this as admin privileges?
      'assoc',                            // command
      ['.ext=CustomExt'],                 // args
      {'shell': true }                    // options: run this into the shell
  );
  
  assoc.stdout.on('data', (buffer) =&gt; {
      var message = buffer.toString('utf8');
      console.log(message);
  });
  
  assoc.stderr.on('data', (data) =&gt; {
      console.log(`stderr: ${data}`);
  });
  
  assoc.on('close', (code) =&gt; {
      console.log(`child process exited with code ${code}`);
  });
  </code></pre>
  <p>Any other way to create an extension association within Nodejs would be appreciated as well.</p>
  <h2>Update 21 Jan 2020</h2>
  <p>I believe that VSCode can do this. So I would need to explore its source code to find the solution. Now I am in other projects, so I do not have time to research for a while. When a file should be saved with root priviledges a message like this is prompted, and that's exactly what I need:</p>
  <p><a href="https://i.stack.imgur.com/eEIWU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eEIWU.png" alt="vscode prompt" /></a></p>
  <h2>Update 24 June 2021</h2>
  <p>I have found a <a href="https://github.com/microsoft/vscode/issues/1614#issuecomment-351078368" rel="nofollow noreferrer">useful thread in VSCode issues</a>. Using <a href="https://github.com/jorangreef/sudo-prompt" rel="nofollow noreferrer"><code>sudo-prompt</code></a> could be the solution. I haven't tried it yet, but the dependency is still in the <a href="https://github.com/microsoft/vscode/blob/main/package.json" rel="nofollow noreferrer"><code>package.json</code></a>, so it looks promising.</p>