I'm trying to open a URL in the browser however the only options I have seen require you have a 'http' within the URL. Im trying to open a localhost from browser. How could I achieve this using nodejs?
Asked
Active
Viewed 98 times
0
-
In a Windows system, you can execute the node statement `child_process.exec('explorer "http://localhost/..."');` – Heiko Theißen Mar 05 '23 at 08:17
-
check this question https://stackoverflow.com/questions/8500326/how-to-use-nodejs-to-open-default-browser-and-navigate-to-a-specific-url – dom1 Mar 05 '23 at 08:25
-
@dom1 the issue with open is that it requires an HTTP URL, Im trying to access a 'non-http' url ie "localhost:3000", no protocol nothing – Dmitriy_I Mar 05 '23 at 20:22
1 Answers
1
Use the open
npm package
Install it:
npm install open
In your node code:
const open = require('open');
open("localhost:8080");

ProfDFrancis
- 8,816
- 1
- 17
- 26
-
localhost is not based on http protocol. Im trying to access my own system not some website localhost. – Dmitriy_I Mar 05 '23 at 20:20
-
-
1this is what I was doing previously. I retried it and it did not work. (I have already tried specifying different browsers as well) – Dmitriy_I Mar 05 '23 at 22:05
-
Hmmmm.... Can you show a screenshot of it working when you directly open it in your browser, and then another screenshot of it not working when you open it with the above method via node? That will make it easier for people to resolve. – ProfDFrancis Mar 05 '23 at 22:39
-
1
-
1I updated everything (npm, node, and chrome). Im not sure which one it was that was causing problems, but it seems to work now. Thank you for the help! – Dmitriy_I Mar 05 '23 at 23:00
-