Get working directory of process by PID - cross platform
npm install pid-cwdNode lib to get the current working directory of given process id - cross platform*
``bash`
$ npm i pid-cwd
`javascript
const pidCwd = require('pid-cwd');
pidCwd(54671)
.then(cwd => {
// A check is recommended, cwd could be null sometimes (read note below)
console.log(cwd); // > /home/chipto/Dropbox
});
`
pid-cwd will simply return null if process doesn't exist, or process is owned by root/administrator or is unreachable or any other reason.EPERM
It won't throw any error or anything like that. It does throw if it can't recognize the OS or given pid is not a positive number.
\* Works as long as process.platform === win32, linux or darwin. require
Throws if it's not one of those, in that case manually from pid-cwd/lib/$1 where $1 is win, linux or macos
Massive thanks to Giampaolo Rodola' and contributors, for their amazing work on psutil
lib/win_pwdx/ contains 99.97% of code borrowed from psutil/arch/windows/process_info.c
psutil is BSD licensed, see psutil/LICENSE` for more information.