const ffi = require("ffi-napi"); // DWORD SetThreadExecutionState(EXECUTION_STATE esFlags); const kernel32 = ffi.Library("kernel32", { SetThreadExecutionState: ["uint", ["uint"]], }); // Flags from Win32 API const ES_CONTINUOUS = 0x80000000; const ES_DISPLAY_REQUIRED = 0x00000002; // keep screen on // Optional: const ES_SYSTEM_REQUIRED = 0x00000001; // keep system awake function keepAwake() { const r = kernel32.SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED); if (r === 0) throw new Error("SetThreadExecutionState failed"); } function clearAwake() { // Clears previous request for this thread kernel32.SetThreadExecutionState(ES_CONTINUOUS); } process.on("SIGINT", () => { clearAwake(); process.exit(0); }); process.on("exit", clearAwake); keepAwake(); console.log("Display will be kept on. Press Ctrl+C to stop."); setInterval(() => {}, 1 << 30);