Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  124] [ 7]  / answers: 1 / hits: 35526  / 11 Years ago, tue, march 26, 2013, 12:00:00
var sys = require('sys'),
exec = require('child_process').exec;

exec(cd /home/ubuntu/distro, function(err, stdout, stderr) {
console.log(cd: + err + : + stdout);
exec(pwd, function(err, stdout, stderr) {
console.log(pwd: + err + : + stdout);
exec(git status, function(err, stdout, stderr) {
console.log(git status returned ); console.log(err);
})
})
})





cd: null :

pwd: null : /

git status returned
{ [Error: Command failed: fatal: Not a git repository (or any of the parent directories): .git ] killed: false, code: 128, signal: null }





nodeJS exec does not work for cd shell cmd. as you see below, pwd works, git status is trying to work but fails because it is not executed in a git directory, but cd cmd fails stopping further successful execution of other cmds.
Tried in nodeJS shell as well as nodeJS+ExpressJS webserver.


More From » node.js

 Answers
3

Each command is executed in a separate shell, so the first cd only affects that shell process which then terminates. If you want to run git in a particular directory, just have Node set the path for you:



exec('git status', {cwd: '/home/ubuntu/distro'}, /* ... */);


cwd (current working directory) is one of many options available for exec.


[#79362] Sunday, March 24, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jennie

Total Points: 593
Total Questions: 102
Total Answers: 106

Location: Federated States of Micronesia
Member since Fri, Sep 16, 2022
2 Years ago
jennie questions
;