why does running `which` in a node child process result in different results?

Viewed 48

when running the which command in terminal (for example, which yarn), I get a different result from when I'm running a node script (from the same location) which calls execSync('which yarn')

can someone explain why?

tldr;

// in terminal
which yarn
// results in 
/Users/xxx/.nvm/versions/node/v17.1.0/bin/yarn

// in node
execSync('which yarn')
// results in
/var/folders/0j/xxx/T/yarn--xxx/yarn
1 Answers

It looks like the Node.js process is running as a different user (not as you), and that user has a different path from your account (or at least, it isn't running any .bashrc or similar specific to your user account that might add to the path). That makes sense given that your result refers to a folder specific to you (/Users/xxx/), but the one from Node.js refers to a central location shared by all users.

Related