Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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.

like image 542
Marco Avatar asked Jan 25 '14 21:01

Marco


1 Answers

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.

like image 164
Trevor Avatar answered Oct 11 '22 09:10

Trevor