Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJs child_process working directory

I am trying to execute a child process in a different directory then the one of its parent.

var exec = require('child_process').exec;  exec(     'pwd',     {         cdw: someDirectoryVariable     },     function(error, stdout, stderr) {         // ...     } ); 

I'm doing the above (though of course running "pwd" is not what I want to do in the end). This will end up writing the pwd of the parent process to stdout, regardless of what value I provided to the cdw option.

What am I missing?

(I did make sure the path passed as cwd option actually exists)

like image 572
Jeroen De Dauw Avatar asked Sep 19 '13 12:09

Jeroen De Dauw


People also ask

How do I get the current working directory in node JS?

There are two ways you can get the current directory in NodeJS: Using the special variable __dirname. Using the built-in method process. cwd()

How do I change the working directory in node JS?

There is no built-in method for Node to change the CWD of the underlying shell running the Node process. You can change the current working directory of the Node process through the command process. chdir() . When the Node process exists, you will find yourself back in the CWD you started the process in.

What is child_process spawn?

spawn() or child_process. spawnSync() . child_process. exec() : spawns a shell and runs a command within that shell, passing the stdout and stderr to a callback function when complete.


1 Answers

The option is short for current working directory, and is spelled cwd, not cdw.

var exec = require('child_process').exec; exec('pwd', {   cwd: '/home/user/directory' }, function(error, stdout, stderr) {   // work with result }); 
like image 95
hexacyanide Avatar answered Sep 19 '22 08:09

hexacyanide