Cannot run npm in a shell script

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.


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:

  • Unable to remove global package

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.