How to open browser from Visual Studio Code API
You don't need to install an external dependency. Opening an URL can be simply done with just the Node.js libraries and system commands. For e.g. use the child_process.exec to open an URL.
Declare a function like so:
const exec = require('child_process').exec;
function openURL(url) {
let opener;
switch (process.platform) {
case 'darwin':
opener = 'open';
break;
case 'win32':
opener = 'start';
break;
default:
opener = 'xdg-open';
break;
}
return exec(`${opener} "${url.replace(/"/g, '\\\"')}"`);
}
and then call it from your code
openURL('http://stackoverflow.com');
There is no need for node or external libraries.
If you are using VS Code 1.31+, use the vscode.env.open
function:
import * as vscode from 'vscode';
vscode.env.openExternal(vscode.Uri.parse('https://example.com'));
For older VS Code versions, use the vscode.open
command:
vscode.commands.executeCommand('vscode.open', vscode.Uri.parse('https://example.com'))
Both of these will open the page in the user's default browser