I am building a node.js application. For executing a mand, I am using child-process. I have to need to change CWD(current working directory) before executing the mand. so I have code like below.
const exec = require('child_process').exec;
let opts = {
"maxBuffer": 1024 * 5000,
"timeout": toolTimeout,
"cwd": /opt/somepath,
"env": {
"PATH": process.env.PATH + ':' + /opt/somepath,
"LANG": "en_US.UTF-8"
}
};
exec(mand, opts, function(error, stdout, stderr) {
}
but above setup is not working as its still not changing CWD before executing mand and mand is executing from current directory where node file resides. Please help, if someone has any idea.
I am building a node.js application. For executing a mand, I am using child-process. I have to need to change CWD(current working directory) before executing the mand. so I have code like below.
const exec = require('child_process').exec;
let opts = {
"maxBuffer": 1024 * 5000,
"timeout": toolTimeout,
"cwd": /opt/somepath,
"env": {
"PATH": process.env.PATH + ':' + /opt/somepath,
"LANG": "en_US.UTF-8"
}
};
exec(mand, opts, function(error, stdout, stderr) {
}
but above setup is not working as its still not changing CWD before executing mand and mand is executing from current directory where node file resides. Please help, if someone has any idea.
Share Improve this question edited Apr 4, 2018 at 7:10 prashant sindhu asked Apr 4, 2018 at 6:14 prashant sindhuprashant sindhu 1,8912 gold badges18 silver badges25 bronze badges 3-
1
I think you pass an absolute path to the cwd option. Use
path.resolve()
to generate an absolute path and pass it. – eskawl Commented Apr 4, 2018 at 7:02 - yes, i am passing absolute path,so you are suggesting to pas abs_path = path.resolve(some_path) ? – prashant sindhu Commented Apr 4, 2018 at 7:06
-
yes, generate an absolute path of cwd and pass it to
exec
– eskawl Commented Apr 4, 2018 at 8:07
3 Answers
Reset to default 4the code snippet you pasted should work and CWD should change current directory as per your value.
though i am suspecting as you are explicitly setting env.
"env": {
"PATH": process.env.PATH + ':' + /opt/somepath,
"LANG": "en_US.UTF-8"
}
you may override all other value of env but you are just setting PATH and LANG in env. Overriding env maybe harmful as it may contain several other variables and values, which may finally cause to failure of any dependent mand.
try to do it like below.
let opts = {
"maxBuffer": 1024 * 5000,
"timeout": toolTimeout,
"cwd": /opt/somepath,
"env": process.env
};
opts.env.PATH = opts.env.PATH + ':' + nlu_plmtool_dir;
exec(mand, opts, function(error, stdout, stderr) {
}
by doing this you will set PATH in env and preserve already set value in env too.
I've had the same issue. This is how I sorted it out
exec('cd newDirectoryName && someOtherCommandForThatDirectoru' , (error, stdout, stderr)=>{...}
This way you can do all that in just on mand.
May be helpful. Thanks
You might want to refer to a similar use-case of yours here. You could also go ahead and use shelljs for capturing the path.