chrisl-921fb74d
chrisl-921fb74d

Reputation: 23100

nodejs open chrome in window with arguments

Trying to write my own plugins for gulp. So I've written a gulp task that attempts opens the chrome browser in windows (i'll work on getting working for mac/linux later).

It seems to work except it's not passing in my arguments:

/*
 * Open
 */

gulp.task('open', function (done) {
  var uri = 'http://localhost:' + CONFIG.PORT,
       CONFIG.PORT = 8080,

  args = [
    uri,
    '--no-first-run',
    '--no-default-browser-check',
    '--disable-translate',
    '--disable-default-apps',
    '--disable-popup-blocking',
    '--disable-zero-browsers-open-for-tests',
    '--disable-web-security',
    '--new-window',
    '--user-data-dir="C:/temp-chrome-eng"'
  ]
  cp.spawn('C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe', args);
})

How do i get it to accept my arguments it passed in? Am in providing it the wrong arguments?

Upvotes: 0

Views: 2470

Answers (1)

Henrik Karlsson
Henrik Karlsson

Reputation: 5713

I would recommend using a quite popular npm module, opener, instead which will solve both your issue with arguments and cross platform support.

Instead of finding the browser executable like you are doing, you can simply write:

var opener = require('opener')
opener('http://google.com')

If you however want to go with your current method, try capturing the output by naming your process and then listening on stderr and stdout:

var chrome = cp.spawn ...

chrome.stdout.on('data', function (data) {                                      
    console.log(data.toString())
})

chrome.stderr.on('data', function (data) {                                      
    console.error(data.toString())
})

It does work for me on linux if I replace your chrome path with chromium.

Upvotes: 3

Related Questions