Unable to use "node" command via SSH - node.js

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

Related

Cypress: Running Test using custom TZ value in Windows

I had my tests designed to be run in an environment which assumed to be in UTC.
I used cy.clock() to set a date-time in UTC format. The Web App to test will display current browser date-time in UTC format.
I had TZ=UTC in my environment variables and I run my test in Cypress Runner in Linux machine using TZ=UTC npx cypress open. The date and the time display on the web app is same as the date time I set using cy.clock()
However, the same method seems doesn't work in Windows. I couldn't use TZ=UTC npx cypress open in Windows. Instead I used npx cypress open. The time is correct but the date differs by one day. If I change my windows pc timezone to UTC, the both date and time is as expected.
Is there any other way other than change the computer timezone in Windows ? I do not need to do so in Linux
My tests runs fine in CI without the need for TZ before the script. Setting it in environment variable in cypress.json is sufficient.
Can someone please enlightened me on this please ?
My cypress.json
{
"projectId": "blahblahbah",
"baseUrl": "http://localhost:4200",
"numTestsKeptInMemory": 10,
"chromeWebSecurity": false,
"TZ": "UTC",
"env": {
"BASE_URL": "http://localhost:4200",
"TZ": "UTC",
},
"integrationFolder": "cypress/tests/",
"retries": {
"runMode": 2,
"openMode": 0
},
"reporter": "cypress-mochawesome-reporter",
"reporterOptions": {
"charts": true,
"reportPageTitle": "XXXX Test",
"embeddedScreenshots": true,
"inlineAssets": true,
"reportDir": "cypress/reports/",
"timestamp": "mmddyyyy_HHMMss"
},
"video": false
}
I think the Windows equiv to TZ=UTC npx cypress open is set TZ=UTC && npx cypress open.
There's also cross-env, allows a single script to work across different OS.
// package.json
{
"scripts": {
"cy:open": "cross-env TZ=UTC npx cypress open"
}
}
But it seems to be fussy about the shell
Windows Issues
Please note that npm uses cmd by default and that doesn't support command substitution, so if you want to leverage that, then you need to update your .npmrc to set the script-shell to powershell.
Another option is run-script-os
See Run different NPM scripts depending on the platform
// package.json
{
"scripts": {
"test": "run-script-os",
"test:win32": "set TZ=UTC && npx cypress open",
"test:darwin:linux": "TZ=UTC npx cypress open",
}
}
I found a workaround in Windows other than setting your computer clock.
You can use WSL2 to run Linux command in Windows 11. If you do not have Windows 11, update it. If your computer cannot support Windows 11, buy a new one.
Make sure you are in Windows 11 Build 22000
Install Docker. Followed the instruction to get Docker running successfully. That also includes setting up WSL2. In this step you will be directed to Microsoft guide to install WSL 2 and install a Linux distro from Microsoft Store. Make sure you did RESTART
Install driver for vGPU
Intel GPU driver for WSL
AMD GPU driver for WSL
NVIDIA GPU driver for WSL
Links can be obtained here: https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps
After that Open Powershell as Admin
wsl --update followed by wsl --shutdown
RESTART COMPUTER
Follow the below sub-steps if you wish to try to luck or increase chances of success.
Can do this for non Windows 11 PC
a. Install VcXsrv
(Multiple windows and Start no client is recommended)
On the page that lets you enable extra settings, disable access control
b. In VSCode, open project folder and a new terminal using dropdown. (There is a + button) Look for Ubuntu (WSL). Here you have access to the bash shell.
c. Type the following command in sequence
set DISPLAY variable to the IP automatically assigned to WSL2
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
echo $DISPLAY
# expect something like 192.168.64.1:0.0
sudo /etc/init.d/dbus start &> /dev/null
sudo visudo -f /etc/sudoers.d/dbus
After this command, an editor will launch. Add the following line with your distro username.
<your_username> ALL = (root) NOPASSWD: /etc/init.d/dbus
Remember to set permission in firewall to allow VcXsrv on domain, private and public network.
RESTART COMPUTER
In VSCode, open project folder and a new terminal using dropdown. (There is a + button) Look for Ubuntu (WSL). Here you have access to the bash shell.
Install cypress dependencies by using
sudo apt-get install libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb
Install npm and nodejs by using
'''
sudo apt-get install npm nodejs
'''
Note: Prior to 3 and . It is assumed that you have already done apt-get update
npm i
Open another New Terminal ( Ubuntu WSL) to run npm serve
You can attempt to
npx cypress open
to see if it works well.
Remember npm serve, npm i and npx cypress open need to be in bash ( Ubuntu WSL)
If you are using Windows 11 Build 22000 and above, you can try to exit VcXsrv, and do npx cypress open again. I tried and it still work.
Reference:
https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps
https://shouv.medium.com/how-to-run-cypress-on-wsl2-989b83795fb6
ANOTHER KISS SOLUTION: USE GIT BASH
Just launch Git Bash from VSCODE followed by TZ=UTC npx cypress open

Running a bash script (with some npm commands in it) in Ubuntu by double-clicking on the bash-file runs into `npm: command not found` error

I am currently trying to set up my system so I can run my bash file (startWebApp.bash) on Ubuntu by double-clicking on it. Unfortunately, that is not working, but when I run the script in the terminal with ./startWebApp.bash it works fine.
The underlying problem seems to be that the $PATH variable is different when running the script by double-clicking on the file. The $PATH variable when I run the file in my Terminal is:
/home/magonba/.nvm/versions/node/v14.17.6/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
While when I run the file by double-clicking on it, $PATH is:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Why is there a difference and how can I change that?
Reproduction steps:
Create the file startWebApp.bash with the following content:
#!/bin/bash
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
npm start &
cd /home/my/project/folder/vuejsapp
npm run serve &
I set in the File Manager under Preferences -> Behavior -> Executable Text Files to Ask what to do (in order to be able to start the application by double-clicking on it).
When I double-click on the file, Ubuntu offers me the options: Run in Terminal, Display, Cancel and Run
I choose Run in Terminal (because I need to provide a password since I am using a sudo command)
Runs into the error(s):
/home/my/project/folder/nodejsapp/startWebApp.bash: line 6: npm: command not found
/home/my/project/folder/vuejsapp/startWebApp.bash: line 8: npm: command not found
Thanks for any help in advance!
The problem you got here is, that nvm is integrated in you .bashrc. But when you run your bash file from the GUI, your bashrc will be ignored, and a very minimal bash shell is started. You can fix this in multiple ways.
Install npm to a system installation path (e.g. with apt)
Change your $PATH in the script
#!/bin/bash
PATH="/home/magonba/.nvm/versions/node/v14.17.6/bin:$PATH"
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
npm start &
cd /home/my/project/folder/vuejsapp
npm run serve &
Every time you call npm, give the full path
#!/bin/bash
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
/home/magonba/.nvm/versions/node/v14.17.6/bin/npm start &
cd /home/my/project/folder/vuejsapp
/home/magonba/.nvm/versions/node/v14.17.6/bin/npm run serve &
Include your .bashrc in your script
#!/bin/bash
. /home/magonba/.bashrc
sudo service postgresql restart
cd /home/my/project/folder/nodejsapp
npm start &
cd /home/my/project/folder/vuejsapp
npm run serve &
Please note.
If you go for solution 2 or 3, every time you change your npm version via nvm, you will have to change your script accordingly.

The problem with running version managed Node as a remote SSH script

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 ?

Running electron as root on Linux

I am making an application with electron which uses the wiring-pi library. This needs access to the GPIO on my Raspberry Pi, which requires root.
When I run electron . in the folder, the app opens fine but then says (in the terminal):
wiringPiSetup: Must be root. (Did you forget sudo?)
However when I try sudo electron ., I get an error:
sudo: electron: command not found
Does anyone know why this is happening?
Also, for the record, the same thing happens when I run npm as root:
pi#raspberrypi:~/rubiks-robot $ sudo npm
sudo: npm: command not found
Any ideas of how I can fix this issue and run Electron as root?
it looks like a problem of environment variables. The environmnet variables are set for your user but not for root.
Try to ship your variables with the "-E" switch of the sudo command:
sudo -E command
Please try to see here for other similar questions
How to keep Environment Variables when Using SUDO

Running protractor from Jenkins

I am trying to run protractor on windows from the command line.
If I directly type the command:
webdriver-manager start
It works great.
However, from within jenkins, it keeps throwing:
'webdriver-manager' is not recognized as an internal or external command,
operable program or batch file.
My env vars seems ok, since the java and npm works fine from Jenkins.
Any idea?
As it turned out I simply forgot putting the C:\Users\USERNAME\AppData\Roaming\npm folder on the System variables and only configured it as my user path.
See elgalu's answer for your next problem though...
Install Protractor globally in the Jenkins machine, for this you may need to ssh into the Jenkins machine that run the tests:
ssh your-jenkins-server
npm -g install protractor
# update webdriver in the Jenkins machine
webdriver-manager update
# test it works
webdriver-manager start
Once you have that setup and running your next problem will probably be running Protractor headless unless your Jenkins machine has a display available and an installed windows manager like Ubuntu Desktop.

Resources