0

I'm writing a script which at one point starts a simple node.js based website:

cd ../ap-site/ && npm start

When I run the above command from terminal there is no problem, but when run from my script I get:

/usr/bin/env: node: No such file or directory

I'm using nvm to manage node versions currently, so I tried adding nvm use 8.8.1 to my ~/.bashrc and then sourcing the file in my script directly:

source /home/rescue/.bashrc
cd ../ap-site/ && npm start

I also tried using which npm in terminal and using the resulting path directly like:

cd ../ap-site/ && /home/rescue/.nvm/v8.8.1/bin/npm start

But I got the same result.

Any ideas on how I can debug the situation and run node projects from my bash script?

0

Apologies for posting the question and answer so quickly, but maybe it will be useful for someone.

Though I was sourcing bash nvm didn't seem to load, despite it being in my .bashrc file.

Instead of sourcing my .bashrc I setup nvm at the top of the script directly:

export NVM_DIR="/home/rescue/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
nvm use 8.8.1

Once I did this my npm commands started working.

I'm still not sure why sourcing .bashrc didn't work as expected, since it contains the same 3 lines of code.

It might be relevant that the script is always run with sudo?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.