Cannot run npm in a shell script Cannot run npm in a shell script shell shell

Cannot run npm in a shell script


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.


Don't do what @rsp suggested, rather than hard-coding the versionyou 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.


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