Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

How to open a URL in the browser automatically with Node

Adnan Abbas

We can automate the process of launching a browser with a URL using the os module. We will be handling the execution of the task by spawning a child process.

Child processes

A process is an instance of a computer program running on your operating system. It contains the program code and its activity. A child process is a process created by a parent process. Node allows you to create a child process in four different ways: spawn(), fork(), exec(), and execFile().

We will be using the exec() call to launch a browser in a child process. The exec() call buffers the command’s generated output and passes all output to a callback function.

Opening URL

Since there are various Operating Systems, we first need to identify the OS to run the OS-specific command.

We will be taking the URL through the terminal as an argument.

const { platform } = require('os');
const { exec } = require('child_process');

const WINDOWS_PLATFORM = 'win32';

const osPlatform = platform(); 
const args = process.argv.slice(2); 
const [url] = args;

Next, we will add some conditional logic to run the executable command based on the OS.

if (url === undefined) {
  console.error('Please enter a URL, e.g. "http://www.opencanvas.co.uk"');
  process.exit(0);
}

let command;

if (osPlatform === WINDOWS_PLATFORM) {
  command = `start microsoft-edge:${url}`;
} else if (osPlatform === MAC_PLATFORM) {
  command = `open -a "Google Chrome" ${url}`;
} else {
  command = `google-chrome --no-sandbox ${url}`;
}
console.log(`executing command: ${command}`);

exec(command);

In the code above, if there is no URL provided in the arguments, the subprocess exits as shown in line 3.

We open the URL in Microsoft Edge if the OS platform is Windows and in Chrome; otherwise if the platform is MAC or some other system.

Once this is done, the command is executed in a child process.

Note: We are using the --no-sandbox in our command to open the browser on the Educative platform. If you are running the script on your local machine, you might not need it in your command.

Example

The following example shows the code of all the steps we have discussed so far.

const { platform } = require('os');
const { exec } = require('child_process');

const WINDOWS_PLATFORM = 'win32';
const MAC_PLATFORM = 'darwin';

const osPlatform = platform();
const args = process.argv.slice(2);
const [url] = args;

if (url === undefined) {
  console.error('Please enter a URL, e.g. "http://www.opencanvas.co.uk"');
  process.exit(0);
}

let command;

if (osPlatform === WINDOWS_PLATFORM) {
  command = `start microsoft-edge:${url}`;
} else if (osPlatform === MAC_PLATFORM) {
  command = `open -a "Google Chrome" ${url}`;
} else {
  command = `google-chrome --no-sandbox ${url}`;
}

console.log(`executing command: ${command}`);

exec(command);
Opening URL in a browser

You can copy and paste the following command in the terminal to run the code on your local machine:

node open.js "http://www.opencanvas.co.uk"

This command provides the URL as an argument, as we discussed above.

RELATED TAGS

node.js

CONTRIBUTOR

Adnan Abbas
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring