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

Author by
Agneli
Updated on February 13, 2020Comments
-
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 about 10 yearsThanks Eugene, but i want get screen resolution of the server same.
-
Eugene Naydenov about 10 yearsIf 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 over 5 yearsIn 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 almost 3 yearsThis entire module is just ``` const robot = require("robotjs") module.exports = robot.getScreenSize() ``` And robotjs does not support multi-monitor btw.
-
Richie Bendall almost 3 years@Arbiter How would you like it to handle multi-monitor support?
-
Arbiter almost 3 yearsI would like it to tell me the number of monitors and the widths and heights of each monitor?