Reputation: 5109
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.
Upvotes: 17
Views: 11236
Reputation: 2355
Escape space with \ too:
C:\\Program\ Files\\Common\ Files\\Oracle\\Java\\javapath\\java.exe
Upvotes: 0
Reputation: 555
As per https://github.com/nodejs/node/issues/7367#issuecomment-229728704 one can use the { shell: true }
option.
For example
const { spawn } = require('child_process');
const ls = spawn(process.env.comspec, ['/c', 'dir /b "C:\\users\\Trevor\\Documents\\Adobe Scripts"'], { shell: true });
Will work.
Upvotes: 7
Reputation: 2357
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: '...'});
Upvotes: 17
Reputation: 6487
I am using spawn
frequently, the way I solved the problem is to use process.chdir
. So if your path is c:\users\marco\my documents\project\someexecutable
then you should do the following:
process.chdir('C:\\users\\marco\\my documents\\project');
child_process.spawn('./myBigFile.exe', options.args);
Note the double \
s, that's how it worked for me.
Upvotes: 1