B T
B T

Reputation: 60915

How to get the cwd (current working directory) from a nodejs child process (in both windows and linuxish)

I'm trying to run a script via nodejs that does:

cd ..
doSomethingThere[]

However, to do this, I need to executed multiple child processes and carry over the environment state between those processes. What i'd like to do is:

var exec = require('child_process').exec;
var child1 = exec('cd ..', function (error, stdout, stderr) {
  var child2 = exec('cd ..', child1.environment, function (error, stdout, stderr) {
  });
});

or at very least:

var exec = require('child_process').exec;
var child1 = exec('cd ..', function (error, stdout, stderr) {
  var child2 = exec('cd ..', {cwd: child1.process.cwd()}, function (error, stdout, stderr) {
  });
});

How can I do this?

Upvotes: 46

Views: 65426

Answers (4)

sjt003
sjt003

Reputation: 2607

I think the best bet is manipulating the options.cwd between calls to exec. in exec callback this.pwd and this.cwd might give you leverage for your implementations.

Upvotes: 0

surfbuds
surfbuds

Reputation: 555

If you want to get the current working directory without resorting to OS specific command line utilities, you can use the "battled-tested" shelljs library that abstract these things for you, while underneath using child processes.

var sh = require("shelljs");
var cwd = sh.pwd();

There you have it, the variable cwd holds your current working directory whether you're on Linux, Windows, or freebsd.

Upvotes: 25

Andrey Sidorov
Andrey Sidorov

Reputation: 25456

to start child with parent dir as cwd:

var exec = require('child_process').exec;
var path = require('path')

var parentDir = path.resolve(process.cwd(), '..');
exec('doSomethingThere', {cwd: parentDir}, function (error, stdout, stderr) {
  // if you also want to change current process working directory:
  process.chdir(parentDir);
});

Update: if you want to retrieve child's cwd:

var fs = require('fs');
var os = require('os');
var exec = require('child_process').exec;

function getCWD(pid, callback) {
  switch (os.type()) {
  case 'Linux':
    fs.readlink('/proc/' + pid + '/cwd', callback); break;
  case 'Darwin':
    exec('lsof -a -d cwd -p ' + pid + ' | tail -1 | awk \'{print $9}\'', callback);
    break;
  default:
    callback('unsupported OS');
  }
}

// start your child process
//    note that you can't do like this, as you launch shell process 
//    and shell's child don't change it's cwd:
// var child1 = exec('cd .. & sleep 1 && cd .. sleep 1');
var child1 = exec('some process that changes cwd using chdir syscall');

// watch it changing cwd:
var i = setInterval(getCWD.bind(null, child1.pid, console.log), 100);
child1.on('exit', clearInterval.bind(null, i));     

Upvotes: 46

Bret Copeland
Bret Copeland

Reputation: 24000

Just a thought, if you know the child process's PID, and have pwdx installed (likely on linux), you could execute that command from node to get the child's cwd.

Upvotes: 2

Related Questions