+ 1
Writing Code
What is the code for shutting down my pc in js.
1 Odpowiedź
+ 2
The following script works on my Windows 10 PC.
Here are some important details:
- You need to run it in Administrator Mode.
- Don't name the script "shutdown.js" or Windows will try to run shutdown.js as JScript instead of running the Windows shutdown command.
- It may take up to a minute to actually shut down but no user-interaction is needed to approve it.
- I tested with the command "node shutdown_script.js" in admin mode and it worked.
const { exec } = require("child_process");
exec("shutdown -s", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
You can not shut down a computer using browser-based JavaScript. If you could ever do that in a browser, it would be an urgently fixed security bug in that browser.