Why installed Node.js and Yarn is lost after reconnect to EC2 linux instance - node.js

I have on AWS EC2 instance. When I install Node.js with this
AWS tutorial all works fine until i logout current session and log in, than Node.js and Yarn are gone. There is also note from AWS:
The node installation only applies to the current Amazon EC2 session. If you restart your CLI session you need to use nvm to enable the installed node version. If the instance is terminated, you need to install node again. The alternative is to make an Amazon Machine Image (AMI) of the Amazon EC2 instance once you have the configuration that you want to keep, as described in the following topic.
Is it possible to install Node.js so it is available in another session? I installed zsh and that works for all sessions.
I also created simple script that will install Node.js and Yarn. Commands in this script are from AWS Tutorial page that I mentioned early. It will install Node.js and Yarn, show that all works (it shows Yarn version and message from node command), but when I type npm --version or yarn --version it will shows message zsh: command not found: yarn (or similar message for npm).
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install --lts
node -e "console.log('Running Node.js ' + process.version)"
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
echo "installing YARN !!!!! "
npm install yarn --global
Why is my script not working?

If you use manual from AWS how to install Node.js than instalation is only made in your home directory that lives only for current session. To install Node.js and Yarn I used this post: https://stackoverflow.com/a/35165401/78935
Or you could simply use this:
sudo curl --silent --location https://rpm.nodesource.com/setup_16.x | bash
sudo yum -y install nodejs
sudo npm install yarn --global
yarn --version

Related

/lib64/libm.so.6: version `GLIBC_2.27' not found (required by node)

When I push the code to Github it automatically builds it and start the application on AWS through CodeDeploy with a application_start.sh script file that has the following code:
#!/bin/bash
#give permission for everything in the express-app directory
sudo chmod -R 777 /directory/backend
#navigate into our working directory where we have all our files OR exit
cd /directory/backend || exit
#add npm and node to path
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # loads nvm bash_completion
#install node modules
npm install
npm install pm2#latest -g
#pm2 stop all
pm2 stop backend
pm2 start ecosystem.config.js --env prodaws
This is the error it shows
127 exit code is "File or Directory Not Found"
I have a nother script that runs before the application_start.sh that installs node, I tried installing a more stable version with nvm install --lt but it did not change anything even the node verison
The only solution to this is to change the Operating System, Amazon Linux 2 does not upgrade those libraries to the latest version.
Changing the OS was also suggested by the Amazon Support team check out this article where I described in detail what happened.

Docker node installation

I'm trying to prepare a docker image to speed-up the building process and to avoid installing various tools and libs every build, my base image will contain it and then I will use it with all installed things I need.
So the problem is that I'm trying to install node from NVM (Node Version Manager) but after I install I cannot use nor nvm nor npm command.
My base image is golang:1.13.1 and I do the following things.
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
Then among tutorials, I saw that ~/.nvm/nvm.sh must be run to finish the job like below
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
I changed it a little bit because my docker image does not recognize few commands due to different shells.
But when I do two separate RUN, the second one does not see NVM_DIR anymore. Of course, I can do everything in one RUN but I need to have npm later on, so each RUN should be able to see this command.
Also, I tried exporting NVM_DIR but it still does not work even when I restart with . ~/.bashrc.
The point of having NVM is that I don't want to care about the node version. Each build will be a LTS version and this is OK for me.
Sharing your solution or advice is welcome. Thanks in advance
The way to install nodejs with npm without changing image and without nvm (which I don't really like) is
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - \
&& apt-get install -y nodejs
After RUN npm -v and RUN node -v it's the same as nvm's LTS version so 6.9.0 and 10.16.3.
I couldn't find better solution but I hope it may help someone

Running npm install on Ubuntu with Octopus Deploy

We are using Octopus deploy to deploy an angularjs app. I'm running a post deployment script (bash) and in there I try to do npm install. This doesn't work, I get an error
npm: command not found
However if I login to the linux box as the Octopus user, go to the directory that Octopus Deploy is trying to run the script from, I am able to run npm install without error.
I've confirmed its the right user (running whoami before npm install in the post deployment script).
I've tried adding the npm executable to my PATH variables which didn't work. I've also put the full path to npm in my script which gives me a new error
/usr/bin/env: ‘node’: No such file or directory
Please run the following command as octopus user to access npm for other users.
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
Or use nvm to install node
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
source ~/.bashrc
source ~/.profile
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  
nvm ls-remote
nvm install 8.10.0 
node --version
npm --version
which node
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
https://github.com/creationix/nvm
If you are installing new node version, please do the following steps.
nvm list will show all installed node versions
nvm use v8.10.0
nvm alias default v8.10.0 set default node version for current user

Launch a Node.Js server when the operating system starts

I have a website written in Node.Js that I launch with this command :
cd /var/mywebsite
npm run dev
What should I put in /etc/init.d/rc.local ?
A solution is : https://causeyourestuck.io/2016/04/30/run-node-js-script-startup/
But I don't know how to create the app.js...
Thanks !
you could use continues integration tools such as puppet to help you do accomplish this. You can also integrate it with Jenkins for the full lifecycle.
https://forge.puppet.com/puppet/nodejs
Given that you are on Linux Ubuntu I think the best and most simple option is a CronTab
#crontab -e
#reboot /home/user/startServer.sh
then your shell/bash script should look something like this
#!/usr/bin/bash
node server.js
or
#!/user/bin/bash
npm run dev
Let me know if that helps, or if you run into issues. I haven't used linux in a few months :)
more info: How to run a shell script at startup
I would recommend a process manager like pm2.
npm i -g pm2
#install pm2
pm2 start <in your project directory>
#starts the project (can start multiple)
pm2 startup
#setup init scripts to start processes on reboot
pm2 save
#saves list of processes
Ref: http://pm2.keymetrics.io/docs/usage/startup/
If you want to use forever-service module, you can use it like this:
Install forever & forever-service packages globally:
sudo npm install -g forever forever-service
Then register your app as a service (default to app.js, but you can specify the name of your app if you need to):
sudo forever-service install your_app --script the_name_of_your_app --start
I have understood the problem and founded the solution !!
A big thank to #Brahma Dev and #TGrif for their help !
The problem was that on the server I have node.js install globally (via apt-get) and an other version installed a the user which launch npm run dev
And if I execute su myuser -c "node -v, su doesn't execute .bashrc before node -v.
So if I execute su myuser and then node -v, the version of node is different !
I have solved the problem by creating an sh script containing :
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
npm run dev
And now, su myuser -c "./start-server.sh" works !
So if I add su myuser -c "/var/www/dialoguea.co.tools/dialoguea/start-server.sh" & in /etc/rc.local the node.js server loads when ubuntu is restarted.

Customize AWS ElasticBeanstalk NodeJS Install (use yarn)

Isit possible to configure EBS to install my NodeJS application using yarn package manager instead of NPM?
I've figured out a way, but it is a little hacky.
Create a .ebextensions/yarn.config file. (The name does not have to be 'yarn'.)
Put this content into the file:
files:
# Runs right before `npm install` in '.../50npm.sh'
"/opt/elasticbeanstalk/hooks/appdeploy/pre/49yarn.sh" :
mode: "000775"
owner: root
group: users
content: |
#!/bin/bash
app="$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)";
# install node
curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -;
# install yarn
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo;
yum -y install yarn;
# install node_modules with yarn
cd "${app}";
yarn --production;
This ebextension creates a file which does 3 things:
Installs node.
Installs yarn.
Installs node_modules with yarn.
In order to make Elastic Beanstalk run yarn install before it runs npm install, the file is created under /opt/elasticbeanstalk/hooks/appdeploy/pre. This turns the file into a pre-deployment hook, which means that Elastic Beanstalk will run it during the first phase of deployment. By default, there is another file in this directory called 50npm.sh, which runs npm install. Since Elastic Beanstalk runs the files in this directory alphabetically, 49yarn.sh (our file) will run before 50npm.sh (the default file), resulting in yarn install running before npm install.
One potential problem is that the environment variables set in the Elastic Beanstalk UI (under Configuration > Software Configuration) are not available at this point of the deployment phase. This is a big problem if you have an npm auth token there which you use to install private npm modules.
Another potential problem is that this installs node manually, so the "Node version" you specify in the Elastic Beanstalk UI (under Configuration > Software Configuration) will have no effect on the version of node your application uses; you need to specify it in this ebextension. Elastic Beanstalk's 50npm.sh both installs node and runs npm install. Since we have to run yarn install before that file runs, we also have to install node manually. Then, when Elastic Beanstalk goes to install node, it detects that node is already installed but does not verify that it is the correct version, so it skips the node installation.
For reference, the yarn installation instructions came from here: https://yarnpkg.com/docs/install#linux-tab
I did this following instructions on https://yarnpkg.com/lang/en/docs/install/
commands:
01_install_yarn:
command: "sudo wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo && curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash - && sudo yum install yarn -y"
This way that i came up with lets you still control the node version via the Elastic Beanstalks Dashboard.
Thanks for this question! couldn't have come to this solution without it :D
"/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh":
mode: "000755"
owner: root
group: users
content: |
#!/usr/bin/env bash
#
# Prevent installing or rebuilding like Elastic Beanstalk tries to do by
# default.
#
# Note that this *overwrites* Elastic Beanstalk's default 50npm.sh script
# (https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663).
"/opt/elasticbeanstalk/hooks/configdeploy/pre/50npm.sh":
mode: "000755"
owner: root
group: users
content: |
#!/usr/bin/env bash
#
# Prevent installing or rebuilding like Elastic Beanstalk tries to do by
# default.
#
# Note that this *overwrites* Elastic Beanstalk's default 50npm.sh script.
# But their default script actually doesn't work at all, since the app
# staging dir, where they try to run `npm install`, doesn't exist during
# config deploys, so ebnode.py just aborts:
# https://gist.github.com/wearhere/de51bb799f5099cec0ed28b9d0eb3663#file-ebnode-py-L140
"/opt/elasticbeanstalk/hooks/appdeploy/pre/49yarn.sh" :
mode: "000775"
owner: root
group: users
content: |
tmp="$(mktemp || bail)";
app="$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)";
version="$(/opt/elasticbeanstalk/bin/get-config optionsettings -n aws:elasticbeanstalk:container:nodejs -o NodeVersion)";
echo $version
major="$(cut -d'.' -f1 <<<${version})"
yum -y install python26 python26-libs
wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo;
wget "https://rpm.nodesource.com/pub_${major}.x/el/7/x86_64/nodejs-${version}-1nodesource.x86_64.rpm" -O "${tmp}";
rpm -i --nosignature --force "${tmp}";
rm -f "${tmp}";
yum -y install yarn;
cd "${app}";
yarn --production;
Had to revisit this as we couldn't figure out why we were stuck on node 8 even though we set it to node 12 in the EB UI. Seems that if you install a global node it overrides the version setting. Instead of installing a global node, this uses the Elastic Beanstalk node install and adds it to the path. You have to add the PATH in again at the start of your yarn install script but it seems to be the least invasive way to use yarn.
content: |
#!/usr/bin/env bash
set -euxo pipefail
EB_NODE_VERSION=$(/opt/elasticbeanstalk/bin/get-config optionsettings -n aws:elasticbeanstalk:container:nodejs -o NodeVersion)
echo "EB node version: $(EB_NODE_VERSION)"
# Make sure Node binaries can be found (required to run npm).
# And this lets us invoke npm more simply too.
export PATH=/opt/elasticbeanstalk/node-install/node-v$EB_NODE_VERSION-linux-x64/bin:$PATH
if yarn -v; then
echo 'Yarn already installed.'
else
echo 'Installing yarn...'
npm install yarn -g
fi
An easy way to prevent EB from running npm install is to create an empty node_modules folder in a prebuild hook:
Edit your_project/.platform/hooks/prebuild/prevent-npm.sh:
#!/bin/bash
# EB build scripts will not install using npm if node_modules folder exists
mkdir node_modules
This way, EB will still install Node.js, so you don't have to install that yourself. But it will skip npm install when node_modules exists.
Then inside the predeploy hook, you can run yarn. If you are running Node.js 16 or newer, you can use corepack yarn, and it will just work (you don't have to install yarn from yum, npm or anything else.)
Edit your_project/.platform/hooks/predeploy/yarn.sh:
#!/bin/bash
corepack yarn
Be sure to also create symlinks under confighooks in your project, or else it will fail to build when changing config (e.g. updating environment values):
mkdir -p .platform/confighooks/{prebuild,predeploy}
ln -s ../../hooks/predeploy/yarn.sh .platform/confighooks/predeploy/yarn.sh
ln -s ../../hooks/prebuild/prevent-npm.sh .platform/confighooks/prebuild/prevent-npm.sh
Since get-config is no longer present in the new Amazon Linux 2 platform, we had to figure another clean way to do this, and came up with the following :
container_commands:
01_npm_install_yarn:
command: "npm install -g yarn"
10_yarn_install:
command: 'PATH="$PATH:$(dirname $(readlink $(which node)))" yarn install'
You may want to put the PATH= logic in a script and call it before every yarn command, to have clean command: instructions in your extentions.
Also, note that if you install yarn using the yum package manager, you completely break the NodeJS version management provided by Beanstalk (since it the black magic running behind make some symlinks in /bin and /usr/bin).

Resources