12

In a continuous deployment context I have a shell script executed to update and restart my app in the remote server

the script is:

ssh user@myserver <<'ENDSSH'
cd /opt/myapp
git pull
npm i
forever stop src
npm run staging
ENDSSH

the output is:

stdin: is not a tty
Already up-to-date.
-bash: line 3: npm: command not found
-bash: line 4: forever: command not found
-bash: line 5: npm: command not found

Note:

  • everything work if I ssh onto the remote server and enter those commands manually

  • node and npm are installed with nvm on the remote server which npm give /root/.nvm/versions/node/v6.10.0/bin/npm

Jordane
  • 624
  • 1
  • 7
  • 23
  • So it looks like you're trying to use npm with nvm. Have you tried running the nvm command in the script to select the version? Before running npm? – wheeler Apr 13 '17 at 14:25
  • @wheeler added `nvm --version` in the script. got `-bash: line 3: nvm: command not found ` – Jordane Apr 13 '17 at 14:27
  • When executed in 'continuos deployment context' which user are you using - the same one that you ssh as? Make sure node is available to CI user by typing `node --version` – GreensterRox Apr 13 '17 at 14:35
  • @GreensterRox got the same problem when I run the script from my computer where node, npm etc. are installed. So the CI is not the source of the problem – Jordane Apr 13 '17 at 14:37

3 Answers3

9

If your node and npm are installed in /root/.nvm/versions/node/v6.10.0/bin then adding this to your script should solve the problem:

PATH="/root/.nvm/versions/node/v6.10.0/bin:$PATH"

Alternatively you can try using absolute paths like:

/root/.nvm/versions/node/v6.10.0/bin/npm install

etc. but note that if you have your Node installed from the binary packages and not from sources then your shebang line in the npm binary will likely be #!/usr/bin/env node which will not work when the correct version of Node in the PATH - see this answer for more info:

When Node was installed from the sources then npm will have a correct shebang line with absolute path to the node binary and can be used wven when node is not in the PATH.

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
6

Don't do what @rsp suggested, rather than hard-coding the version you should source NVM script like this:

. /root/.nvm/nvm.sh

NVM should resolve the version for you if you set the default one, that way you won't be coming back to that script just because you updated to newer NodeJS version.

TheCodeDestroyer
  • 763
  • 9
  • 29
5

I was facing same problem in jenkins.

Following lines was on bottom of .bashrc file , I just put top of .bashrc file

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
Shree Prakash
  • 2,052
  • 2
  • 22
  • 33