NodeJs child_process working directory

Javascriptnode.jsChild ProcessWorking Directory

Javascript Problem Overview


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)

Javascript Solutions


Solution 1 - Javascript

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
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeroen De DauwView Question on Stackoverflow
Solution 1 - JavascripthexacyanideView Answer on Stackoverflow