How to start browser from command line node.js script

Possible duplicate:
How to use nodejs to open a default browser and navigate to a specific url

I don't know if that matters, but I'm on OSX.

I know that you can start the browser from the command line itself by typing:

open http://www.stackoverflow.com 

But is there a way to open a browser from a command line nodejs script?

+43
command-line shell
Oct 05 '11 at 16:30
source share
1 answer

Open exists now, use this. :)

Install with:

 $ npm install --save open 

Use with:

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

app:... option:

Type: string | string[]

Specify the application to open the target or an array with the arguments of the application and application.

The name of the application is platform dependent. Do not write code hard in reusable modules. For example, Chrome is Google Chrome for MacOS, Google Chrome for Linux, and Chrome for Windows.

You can also specify the full path in the application. For example, for WSL, it might be / mnt / c / Program Files (x86) /Google/Chrome/Application/chrome.exe to install Chrome on Windows.

Example:

open('http://localhost', {app: "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"});

+90
05 Oct '11 at 17:08
source share



All Articles