Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a script to connect through ssh then run npm install pm2 results in: 'npm: command not found'

I'm running this command from my local machine:

ssh -tt -i "pem.pem" [email protected] "sudo su -c 'cd /dir/;npm install pm2'"

It connects, operates as a super users, cds to dir and attempts to run the command but returns that npm is not a command recognized by the system.

However, when I connect "manually" i.e.

ssh -i "pem.pem" [email protected]
sudo su
cd /dir
npm install pm2

it works.

npm is installed under root and the system can see it.

ssh -tt -i "pem.pem" [email protected] "sudo su -c 'cd /dir/;whoami'"

and

ssh -i "pem.pem" [email protected]
sudo su
cd /dir
whoami

both return "root"

Why can't the npm command be found when running on top of an ssh?

like image 504
Paul Sender Avatar asked Oct 27 '25 22:10

Paul Sender


1 Answers

When you login, you create an interactive shell, which typically will read a couple of files, including /etc/profile, $HOME/.profile, and $HOME/.bashrc in the case of bash.

Any of these files can add extra elements (paths) to the PATH variable, which affects which commands can be found.

When you run the command line directly, no such initialisation takes place, and the value of $PATH may be limited to just /bin:/usr/bin.

Next there is sudo, which may or may not import the value of PATH when looking for commands.

Solution

Best you can do is find out where npm is installed, and use its full PATH.

like image 165
Henk Langeveld Avatar answered Oct 29 '25 18:10

Henk Langeveld