NodeJs child_process 工作目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18894433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NodeJs child_process working directory
提问by Jeroen De Dauw
I am trying to execute a childprocess 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.
我正在执行上述操作(当然,运行“pwd”并不是我最终想要做的)。无论我向 cdw 选项提供什么值,这最终都会将父进程的密码写入 stdout。
What am I missing?
我错过了什么?
(I did make sure the path passed as cwd option actually exists)
(我确实确保作为 cwd 选项传递的路径确实存在)
回答by hexacyanide
The option is short for current working directory, and is spelled cwd, not cdw.
该选项是当前工作目录的缩写,拼写为cwd, not cdw。
var exec = require('child_process').exec;
exec('pwd', {
cwd: '/home/user/directory'
}, function(error, stdout, stderr) {
// work with result
});

