How to use nodejs to open default browser and navigate to a specific URL

Javascriptnode.js

Javascript Problem Overview


I'm writing an application using Node.js.

One of the functions I want to create is to open the default web browser and navigate to a specific URL.

I want it to be portable so that it runs on Windows/Mac/Linux.

Javascript Solutions


Solution 1 - Javascript

Use open (formerly known as opn) because it will handle the cross platform issue. To install:

$ npm install open

To use:

const open = require('open');

// opens the url in the default browser 
open('http://sindresorhus.com');
 
// specify the app to open in 
open('http://sindresorhus.com', {app: 'firefox'});

Solution 2 - Javascript

var url = 'http://localhost';
var start = (process.platform == 'darwin'? 'open': process.platform == 'win32'? 'start': 'xdg-open');
require('child_process').exec(start + ' ' + url);

Solution 3 - Javascript

node-open is deprecated. Now use open:

const open = require('open')

await open('http://sindresorhus.com') // Opens the url in the default browser

await open('http://sindresorhus.com', {app: 'firefox'}) // Specify the app to open in

Solution 4 - Javascript

You may need to implement a switch using the value of ...

require('os').type()

And then use spawn("open") or spawn("xdg-open") depending on the platform?

Solution 5 - Javascript

Install:

$ npm install open

Usage:

const open = require('open');
 
(async () => {
    // Opens the image in the default image viewer and waits for the opened app to quit.
    await open('unicorn.png', {wait: true});
    console.log('The image viewer app quit');
 
    // Opens the URL in the default browser.
    await open('https://sindresorhus.com');
 
    // Opens the URL in a specified browser.
    await open('https://sindresorhus.com', {app: 'firefox'});
 
    // Specify app arguments.
    await open('https://sindresorhus.com', {app: ['google chrome', '--incognito']});
})();

Solution 6 - Javascript

The easiest and neatest way, IMHO is using an npm package called openurl. Do a npm install openurl . You could try this real quick in your Nodejs REPL

require("openurl").open("http://stackoverflow.com/questions/8500326/how-to-use-nodejs-to-open-default-browser-and-navigate-to-a-specific-url")

You could also send emails with it if the need arises like so; require("openurl").open("mailto:[email protected]")

Solution 7 - Javascript

Windows + Express

app.listen(3000, ()=>{
	require('child_process').exec('start http://localhost:3000/');
});

Solution 8 - Javascript

#!/usr/bin/env node

const url = 'http://localhost'
require('child_process')
  .exec((process.platform
         .replace('darwin','')
         .replace(/win32|linux/,'xdg-') + 'open ' + url));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionQing XuView Question on Stackoverflow
Solution 1 - JavascriptForbesLindesayView Answer on Stackoverflow
Solution 2 - Javascriptlexa-bView Answer on Stackoverflow
Solution 3 - JavascriptOlivier CView Answer on Stackoverflow
Solution 4 - JavascriptSorenView Answer on Stackoverflow
Solution 5 - JavascriptpinoView Answer on Stackoverflow
Solution 6 - JavascriptEL TaipanView Answer on Stackoverflow
Solution 7 - JavascriptVadimView Answer on Stackoverflow
Solution 8 - JavascriptJonathan DobsonView Answer on Stackoverflow