7

I am wondering if it is possible for a node.js program/script to prevent the operating system (windows) from entering in sleep or hibernate mode?

Talha Awan
  • 4,573
  • 4
  • 25
  • 40
Franck Freiburger
  • 26,310
  • 20
  • 70
  • 95

2 Answers2

3

This will help you.

https://www.npmjs.com/package/stay-awake

var stayAwake = require('stay-awake');

// do something not so important 

// prevent auto sleep 
stayAwake.prevent(function(err, data) {
    // handle error 
    console.log('%d routines are preventing sleep', data);
});

// do something which needs the computer to stay awake 

// do something async 
doAsync(function() {
    // do something 
    stayAwake.prevent(function() {}); // second call 
    // do long processing 
    stayAwake.allow(function() {}); // this subroutine no longer needs to prevent sleep 
});

// once done allow the computer to sleep as configured in power management 
stayAwake.allow(function(err, data) {
    if(data == 0) {
        console.log('Will sleep automatically');
    }
}); // allow
Purus
  • 5,701
  • 9
  • 50
  • 89
  • 1
    Linux is not supported with this package. – itsezc Feb 21 '21 at 14:26
  • This plugin does not works anymore with Node 14. (this is my experience). I think that this is the right URL of repository: https://github.com/dapetcu21/node-stay-awake – riofly Sep 06 '21 at 15:48
1

I was used "stay-awake" but it does not works in "Node 14".

Now I use this: https://www.npmjs.com/package/node-prevent-sleep

riofly
  • 1,694
  • 3
  • 19
  • 40