Cannot start npm in shell script

In the context of continuous deployment, I run a shell script to update and restart the application on the remote server.

the script is this:

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

output:

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

Remarks:

  • everything works if I ssh to a remote server and enter these commands manually

  • node and npm are installed using nvm on a remote server, which npmgiveswhich npm /root/.nvm/versions/node/v6.10.0/bin/npm

+9
source share
3 answers

If yours are nodeboth npminstalled 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, for example:

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

... note, Node , , shebang npm, , #!/usr/bin/env node, , Node PATH - . :

Node , npm shebang node wven, node PATH.

+5

, @rsp, NVM script :

. /root/.nvm/nvm.sh

NVM , , script , NodeJS.

+2

I ran into the same issue at Jenkins.

The following lines were at the bottom of the .bashrc file, I just put the top of the .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
+1
source

Source: https://habr.com/ru/post/1674757/


All Articles