How to get screen resolution with node.js

22,656

Solution 1

I solved the problem with phantomJS.

Install phantomJS:

sudo apt-get install phantomjs

Create app.js file:

var w = screen.width;
var h = screen.height;
console.log(w+"x"+h);
phantom.exit();

Execute:

phantomjs app.js

Return:

1366x768

Solution 2

There is now a screenres module via npm:

https://www.npmjs.com/package/screenres

Share:
22,656
Agneli
Author by

Agneli

Updated on February 13, 2020

Comments

  • Agneli
    Agneli over 3 years

    I need get screen resolution with node.js, but the following code don't work.

    var w = screen.width;
    var h = screen.height;
    

    This also don't work.

    var w = window.screen.width;
    var h = window.screen.height;
    

    Someone know how to get screen resolution with node.js ? Thank you.

  • Agneli
    Agneli about 10 years
    Thanks Eugene, but i want get screen resolution of the server same.
  • Eugene Naydenov
    Eugene Naydenov about 10 years
    If you run a browser on the server you can run the client-side script in that browser and send the data to the server-side application in same way.
  • Dominic Cerisano
    Dominic Cerisano over 5 years
    In the common case of Linux servers running multiple displays, X11 defaults internally to a single huge aggregate window. Phantomjs will return that resolution. Eugene's answer will return the resolution of whatever display the browser is running on, regardless of OS, or how its window manager is configured.
  • Arbiter
    Arbiter almost 3 years
    This entire module is just ``` const robot = require("robotjs") module.exports = robot.getScreenSize() ``` And robotjs does not support multi-monitor btw.
  • Richie Bendall
    Richie Bendall almost 3 years
    @Arbiter How would you like it to handle multi-monitor support?
  • Arbiter
    Arbiter almost 3 years
    I would like it to tell me the number of monitors and the widths and heights of each monitor?