Marco Franssen Marco Franssen - 2 months ago 14
Node.js Question

node child_process.spawn not working with spaces in path on windows

How to provide a path to

child_process.spawn


For example the path:

c:\users\marco\my documents\project\someexecutable


The path is provided by the enduser from a configuration file.

var child_process = require('child_process');
var path = require('path');
var pathToExecute = path.join(options.toolsPath, 'mspec.exe');
child_process.spawn(pathToExecute, options.args);


Currently only the part after the space is used by
child_process.spawn


I also tried by adding quotes arround the path like this:

var child_process = require('child_process');
var path = require('path');
var pathToExecute = path.join(options.toolsPath, 'mspec.exe');
child_process.spawn('"' + pathToExecute + '"', options.args);


However this results in a
ENOENT
error.

Answer

The first parameter must be the command name, not the full path to the executable. There's an option called cwd to specify the working directory of the process, also you can make sure the executable is reachable adding it to your PATH variable (probably easier to do).

Also, the args array passed to spawn shouldn't contain empty elements.

You code should look something like this:

child_process.spawn('mspec.exe', options.args, {cwd: '...'});
Comments