'Can Node.js invoke Chrome?

Is it possible for Node.js running on a desktop to spawn a Chrome Browser window? I would like to start a Chrome Browser providing the window size and location when Node.js receives an event.

Is sys shell commands only methodology?



Solution 1:[1]

On MacOSX

var childProc = require('child_process');
childProc.exec('open -a "Google Chrome" http://your_url', callback);
//Or could be: childProc.exec('open -a firefox http://your_url', callback);

A bit more:

Solution 2:[2]

Checkout https://www.npmjs.com/package/chrome-launcher:

Launch chrome:

const chromeLauncher = require('chrome-launcher');

chromeLauncher.launch({
  startingUrl: 'https://google.com'
}).then(chrome => {
  console.log(`Chrome debugging port running on ${chrome.port}`);
});

Launching headless chrome:

const chromeLauncher = require('chrome-launcher');

chromeLauncher.launch({
  startingUrl: 'https://google.com',
  chromeFlags: ['--headless', '--disable-gpu']
}).then(chrome => {
  console.log(`Chrome debugging port running on ${chrome.port}`);
});

chrome-launcher opens a remote debugging port so you can also control browser instance using the DevTools protocol.

Puppeteer is another way to launch Chrome and interact with it using high level APIs.

Solution 3:[3]

With opn:

const opn = require('opn');
opn('http://siteurl.com/', {app: ['google chrome']});

Solution 4:[4]

I open a new firefox tab on windows here: https://github.com/Sequoia/FTWin/blob/master/FTWin.n.js

The most salient portion:

var cp = require('child_process'),
    url_to_open = 'http://duckduckgo.com/';

cp.spawn('c:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe', ['-new-tab', url_to_open]);

Note:

  1. Passing the full path of firefox to child_process.spawn
  2. Escaping of slashes
  3. Passing switches/arguments to firefox.exe: passed as the second parameter of cp.spawn as an array (one entry per switch).

This call is the equivalent of typing "c:\Program Files (x86)\Mozilla Firefox\firefox.exe" -new-tab http://duckduckgo.com at the windows command line.

For chrome you'd want something like D:\Users\sequoia\AppData\Local\Google\Chrome\Application\chrome.exe --new-tab http://duckduckgo.com/ I'll let you work out the child_process version on your own ;)

References:

http://peter.sh/experiments/chromium-command-line-switches/

http://nodejs.org/docs/v0.3.1/api/child_processes.html

Solution 5:[5]

Yeah, I would think you would need to escape out to shell and then open up chrome.

Solution 6:[6]

Node can only do that if you call a UNIX / Windows command, so sys shell command only.

Solution 7:[7]

This can be done using open npm package.

app.listen(PORT, (err) => {
  if (err) console.log(err);
  else open(`http://localhost:${PORT}`, { app: "google chrome" });
});

We can specify any browser in second parameter with open function.

Solution 8:[8]


This answer does not help if you need chrome stored info or chrome per se.

https://github.com/puppeteer/puppeteer https://www.npmjs.com/package/puppeteer

For any case that you don't explicitly need previous local stored info, I suggest using a puppeteer instance. It comprehends all browser resources such as storages, etc

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MaxRd
Solution 2 ebidel
Solution 3 Olivier C
Solution 4 Stop Slandering Monica Cellio
Solution 5 wesbos
Solution 6 alessioalex
Solution 7 Sahil Julka
Solution 8 Allan Felipe Murara