Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node and NPM path in OSX

Tags:

node.js

macos

I have a very strange configuration issue.

When logging to machine (SSH session) and executing the following commands using interactive shell, they succeed:

node --version
npm --v

However, when I execute the following through script from remote machine:

#!/bin/bash

#Script input arguments
user=${1} 
server=${2} 

#Tell the shell to quote your variables to be eval-safe!
printf -v user_q '%q' "$user"
printf -v server_q '%q' "$server"
#

# Script variables
address="$user_q"@"$server_q"
#

function run {
    ssh "$address" /bin/bash "$@"
}


run << SSHCONNECTION

node --version
npm --v

 exit
SSHCONNECTION

I get the following output:

/bin/bash: line 2: node: command not found

/bin/bash: line 3: npm: command not found

I am conscious that the issue may be the in .bash_profile file configuration

export PATH="/usr/local/bin/npm:/usr/local/bin/node:/usr/local/bin:$PATH"

Also, here is result of npm config get prefix:

/usr/local

Can you please suggest what I am doing wrong?

like image 451
Alex Avatar asked Mar 17 '26 22:03

Alex


1 Answers

I believe what happened is due to the fact .bash_profile is only executed when bash is invoked for an interactive shell, more info about this here: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html#Bash-Startup-Files

When invoking and passing the script directly to /bin/bash, it will behave as a non-interactive shell which the default behaviour to not read the .bash_profile.

I would suggest setting you PATH variable in /etc/environment so it will take affect for all users and all manners of scripting.

like image 195
Farid Nouri Neshat Avatar answered Mar 20 '26 13:03

Farid Nouri Neshat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!