My question is about nvm but it may relate to other Node version manages like n or nvs.
You probably a;ready know that you can run remote programs with SSH like this:
ssh user#server COMMAND
For example the command can be the Node.js script:
ssh user#devserver 'node ~/getstats'
The problem is that it will not work for Node that was installed using nvm. Why? Because node is actually an alias to something like /home/user/.nvm/versions/node/v12.1.0/bin/node. The alias is installed in ~/.bashrc which is run when you login with SSH. But when you execute remote command with ssh SERVER COMMAND environment scripts are not run because the shell runs in a restricted mode.
One work around is to create ~/node which containts /home/monitor/.nvm/versions/node/v12.1.0/bin/node * and is executable, then you can do ssh SERVER './node SCRIPT'. But this is not perfect because once you upgrade Node the path will change and you will need to update this file as well.
What would be the recommended way to solve the problem with running version managed Node as a remote SSH script?
Try this:
ssh user#devserver '. ~/.nvm/nvm.sh && node ~/getstats'
Note that by default, .bashrc in Ubuntu 18.04 has the following:
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
Have you tried to run sss user#host nvm run node ~/getstats ?
Related
I'm working on a Raspberry Pi running nextjs (node server) connected with nginx. I tried using x-terminal-emulator ‘/usr/bin/bash -c /home/pi/Desktop/startup.sh’ but many of them didn’t recognize /usr/bin/bash as a directory and most of them didn’t budge to execute the script. In how many ways can I start my bash script? What is the most useful way possible?
These are the ways that I tried and the errors corresponding to them.
contrab -e: Didn’t budge to execute the command line
rc.local: DIdn’t budge to execute the command line
systemd: /usr/bin/bash file or directory doesn’t exist.
I’ve must of been using them incorrectly. If so what is the way to use them?
This recipe allows you to launch node server at system boot. In this recipe it is assumed that you have a node server running on port 3000.
Steps
Add node server to the rc.local file
sudo nano /etc/rc.local
Add the following line to the rc.local file
sudo node /home/pi/node_server.js
Make rc.local executable
sudo chmod +x /etc/rc.local
Reboot the system
sudo reboot
Verify that the server is running at boot
sudo netstat –lntp | grep 3000
This should output node server which is running on port 3000.
Verify that the server is accessible from remote computer
curl -i http://<ip_address>:3000
During a deployment process I run yarn install over an SSH connection in a non-interactive shell. Since an alternative node.js version (default version 8.x on the server is too low) must be used, an alias is created and loaded in the bash script over the SSH connection:
shopt -s expand_aliases
source ~/.bashrc
node -v
The .bashrc script looks like:
export PATH=/opt/plesk/node/12/bin/node:$PATH
alias node="/opt/plesk/node/12/bin/node"
Running node -v will print the correct version 12.x. The correct version is also output when executing JavaScript code:
node -e "console.log(process.versions.node);"
But yarn calls node internally with exec (https://github.com/yarnpkg/yarn/blob/master/bin/yarn#L20). And this will call the default version of node instead of the alias version. That means yarn versions lists node 8.x.
This is only the case with a non-interactive bash. If I call this in an interactive shell, yarn internally uses the correct node version 12.x of the alias.
How can I run yarn in a non-interactive bash with the correct node version?
I have set up a Ubuntu 14.04 server in Virtual box and installed with node.js.
I could run the command "node server.js" normally in the virtual box.
However, when I use my windows 10 command prompt to connect the server by "ssh user#192.168.x.x" and run "node server.js", it shows up:
The program 'node' can be found in the following packages:
* node
* nodejs-legacy
Ask your administrator to install one of them
What should I do?
Most likely due to the way you installed node, it's not in your PATH.
This can be due to logging in with a different user than the one you used to install ( that has different path settings ), or it can be due to how the shell is configured w.r.t. login and non-login shells which in essence govern what configuration scripts are sourced and thus what is the value of $PATH.
You could try to run running with:
echo $PATH
Both directly and trough ssh to compare the configuration.
Try the following:
nodejs server.js
I am trying to have vagrant install all that is needed for nodejs to run properly. Then after doing a 'nohup grunt server &' on the root folder of the node project I was expecting for the server to be up listening at port 3030 but it's not.
If right after provisioning I do
vagrant ssh
grunt server &
all works as it should. It's only when the provisioner runs it that it doesn't work.
Here's part of my Vagrantfile:
git clone https://github.com/airbnb/rendr.git
cd /home/temp/rendr/examples/00_simple/
npm install
cp -rf /home/temp/rendr/examples/00_simple/ /home/website/nodejs/rendr-try1
cd /home/website/nodejs/rendr-try1/00_simple
nohup grunt server &
So it definitely seems to be related to the provisioner not running in a terminal, but is there any easy way to get this running without using something like upstart ?
Ok found this out.
It turns out whatever process that wants to stay running has to be properly daemonized. In order to do this one must detach stdin, stdout and stderr.
I was doing
"nohup grunt server &"
to start node.js through grunt
Replacing it with:
"nohup grunt server 0<&- &>/dev/null &"
works perfectly
I am trying to have multiple connections to my dev server by ssh-ing once into my server and the using screen to open multiple sessions. My problem is that when I run nvm command in screen, the command can not be found. When I exit screen and run the command in the pure ssh terminal it runs fine. I think this is a PATH problem. I wanted to get an absolute path for nvm to see if I can run that in screen.
I ran the code below to find the path
~$ nvm
//SHOWS USAGE INFO
~$ whereis nvm
nvm:
~$ screen
//LOADS INTO SCREEN SESSION
~$ nvm
bash: nvm: command not found
What does this mean for the install of nvm. If I change its install directory could I then run it in screen.
If this is the case then why wouldn't screen run nvm in the first place.
Try manually activating nvm with:
source ~/.nvm/nvm.sh
Try
which nvm
instead of whereis. This will give you the full path.
On startup of a terminal I need to run .nvm.sh before I can execute nvm. I put .nvm.sh into my .bashrc. Turns out that an ssh connection runs the .bashrc but a screen connection does not. This is why the command was not available in screen. I had to manually run .nvm.sh.