Rahul Soni
Rahul Soni

Reputation: 4968

Nodejs ignoring cwd while spawning a child process

I am trying to spawn a child process with a default directory. The process is being executed and it is able to read the args. But the process starts up in the current directory, even though I am providing cwd as options. Am I doing this incorrectly?

var spawn = require('child_process').spawn;
var child = spawn("sh",
   ["path_to_file_name", "args"],
   [{cwd:"/some/path/temp"}]
);

My end goal is to execute a bash script, in a specific folder so that it doesn't mess up my application directory.

Node version : 4.0.0

Upvotes: 4

Views: 7055

Answers (2)

funerr
funerr

Reputation: 8156

Maybe use python-shell, it seems well maintained and easy to use if you need more wrappers around excuting python.

https://github.com/extrabacon/python-shell https://www.npmjs.com/package/python-shell

npm install python-shell

import {PythonShell} from 'python-shell';

PythonShell.runString('x=1+1;print(x)', null).then(messages=>{
  console.log('finished');
});

Upvotes: 0

vinayr
vinayr

Reputation: 11234

options is an object, not array.

var child = spawn("sh",
   ["path_to_file_name", "args"],
   {cwd:"/some/path/temp"}
);

Upvotes: 10

Related Questions