0

I have a CircleCI config that has a deployment script at the end. The command should SSH into the server, move to a directory and execute an NPM script. It looks like this:

ssh -o StrictHostKeyChecking=no ubuntu@xx.xx.xx.xx "cd /var/www/example.com && npm run restart_qa"

This throws the following error:

bash: npm: command not found

This indicates NPM isn't installed on the server, but it is. To test this, if I run the commands separately, they work. e.g.

ssh -o StrictHostKeyChecking=no ubuntu@xx.xx.xx.xx
cd /var/www/example.com
npm run restart_qa

So what's the problem here?

CaribouCode
  • 13,998
  • 28
  • 102
  • 174
  • The npm file should be in /usr/local/bin/npm. Follow this [link](https://stackoverflow.com/questions/31472755/sudo-npm-command-not-found/34558299) – jwatts Jan 16 '19 at 15:18
  • @jwatts I'm not sure why that matters. If I SSH in, then separately run the npm command it works. So npm exists on the server, and it runs with a plain npm command. Why would I need to declare where it lives in the command? – CaribouCode Jan 16 '19 at 15:23
  • 3
    When you ssh interactively, your shell loads a different set of configuration files than when ssh in to run a command. For example, in Bash .bash_profile isn't sourced when running the command from ssh. Check if the npm installer updated your profile with ~/.npm in the PATH. Move that to .bashrc, for example, to make available in both usage scenarios. – bishop Jan 16 '19 at 15:43
  • @bishop Great idea! – CaribouCode Jan 16 '19 at 16:18

1 Answers1

1

Turns out this had something to do with the installed path of npm and node. I used NVM to install both, so they were not in the place the shell script was expecting. To solve this I did the following:

sudo ln -s /home/ubuntu/.nvm/versions/node/v10.13.0/bin/npm /usr/local/bin/npm
sudo ln -s /home/ubuntu/.nvm/versions/node/v10.13.0/bin/node /usr/local/bin/node
CaribouCode
  • 13,998
  • 28
  • 102
  • 174
  • IMHO, you could place a check while running specific command related to npm so that use could get a proper message in case it is not present in target server(ssh one). – RavinderSingh13 Jan 16 '19 at 15:38