I am trying to use the child_process.spawn
function. The syntax is
child_process.spawn(command, args=[], [options])
Whenever I include a space in any of the elements of the args array, the command simply emits the argument. Here is some code I used to test it
var spawn = require("child_process").spawn
console.log("This works");
var watcher = spawn("ls", ["-l"]);
watcher.stdout.on('data', function(data) {
process.stdout.write(data.toString());
});
console.log("This does not work");
watcher = spawn("ls", ["-l", "/path with space in it"]);
watcher.stdout.on('data', function(data) {
process.stdout.write(data.toString());
});
Is this a bug in node? Do I need to escape the space?
Edit: The above code is just an example. Here is the real code. Maybe is has to do with the pipes?
watcher = spawn("supervisor", ["--extensions\ 'coffee|js|css|coffeekup'", "src/app.coffee"]);
Don't put spaces in args
, just use another argument in the array
var watcher = spawn("supervisor", [
"--extensions",
"'coffee|js|css|coffeekup'",
"src/app.coffee"
]);
A handy little shortcut I found if you want to get quick diagnostic output from your child processes is passing the {stdio: "inherit"}
in options
var watcher = spawn("supervisor", [
"--extensions",
"'coffee|js|css|coffeekup'",
"src/app.coffee"
], {stdio: "inherit"});
This way you can see whether everything is working properly right away.
Lastly, depending on where supervisor
is installed, you might want to consider using the full path.
var watcher = spawn("/path/to/supervisor", ...);
Yes, you need to escape it, just as you would command line.
watcher = spawn("ls", ["-l", "/path\ with\ space\ in\ it"]);
I doubt it's a bug in node, since you could do something like ['-option="thing with spaces"']
and you shouldn't expect node to guess when to escape spaces and when not to.
ls a\ b/
blorf
cat a.js
var spawn = require("child_process").spawn
watcher = spawn("ls", ["-l", "./a\ b"]);
watcher.stdout.on('data', function(data) {
process.stdout.write(data.toString());
});
node a.js
total 0
-rw-r--r-- 1 bob staff 0 Jun 7 17:26 blorf