isimmons
isimmons

Reputation: 2026

node.js, windows 7, spawn chrome, can't set user-data-dir

I'm trying to open Google Chrome with node.js on Windows 7 using spawn. It seems to work for all arguments except for setting the user-data-dir. However, --user-data-dir works directly from command line and also works if I call a .cmd file from node, passing the argument through it.

Chrome opens in the background (shows in task manager) but the browser window doesn't open and I have to manually kill the process in task manager to end it.

I get no errors or any indication of what the problem is.

Here is my node code:

var spawn  = require('child_process').spawn;
var exe = 'C:\\Program Files\\Google\\Chrome\\application\\chrome.exe';


//this does not work and gives no errors just starts chrome in the background but fails to open browser window
// var args = [
//      '--user-data-dir=C:\\Windows\\Temp\\testingdir',
//      'http://www.google.com'
//  ];

//actual output from running this through a .cmd file and echoing out the arguments
// --user-data-dir=C:\Windows\Temp\testingdir http://www.google.com
//running the above arguments through a .cmd file from nodejs or manually running them in the cli works



//this works from nodejs, other arguments work too, just not the --user-data-dir argument
// var args = ['http://www.google.com'];

chrome = spawn(exe, args);


chrome.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

chrome.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

chrome.on('exit', function (code) {
  console.log('child process exited with code ' + code);
  // chrome.kill();
});

As you can see in the commented out code it works as long as I don't try to change the user-data-dir. I've tried different temp directories and setting full access to 'everyone' on the temp directory in case it was a permissions issue.

Update: I just updated from v0.8.1 I think to the latest v0.8.14 and now when it fails the on('exit', function()) actually returns a code.

child process exited with code 20

How can I look up this code to find out what it means?

Update 2 The exit code 20 I think was just a one time fluke because I can't reproduce it any more. Trying to test different things is kinda buggy because I have to remember to kill the chrome processes in task manager each time to make sure I get the same results. So it's back to the same results as before, no errors but no chrome window, and some chrome processes running that have to be manually killed.

Update 3 I just moved this script over to Ubuntu and changed the exe and args to reflect the different file structure, and it works on Ubuntu.

exe = "/opt/google/chrome/google-chrome"

and

args = [
    "http://www.google.com",
    "--user-data-dir=/home/lotus/testdir"
    ];

So I can confirm this is specifically related to Chrome on Windows.

Upvotes: 2

Views: 1437

Answers (1)

mihai
mihai

Reputation: 38543

http://www.hiteksoftware.com/mize/Knowledge/articles/049.htm

20 The system cannot find the device specified.

doesn't make much sense though

Upvotes: 1

Related Questions