nodeJS exec does not work for “cd ” shell cmd

后端 未结 3 1922
忘掉有多难
忘掉有多难 2021-02-05 01:04
var sys = require(\'sys\'),
    exec = require(\'child_process\').exec;

exec(\"cd /home/ubuntu/distro\", function(err, stdout, stderr) {
        console.log(\"cd: \" +          


        
3条回答
  •  感动是毒
    2021-02-05 02:07

    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.

提交回复
热议问题