NodeJS not installed successfully in AWS EC2 inside User-data

1

I've tried to install NodeJS in EC2instance - linux as follow inside user-data:

#!/bin/bash                                                  
yum update -y
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
export NVM_DIR="/home/ec2-user/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  
yum install git -y
git config --global credential.helper '!aws codecommit credential-helper $@'
git config --global credential.UseHttpPath true
git clone https://git-xxxx
cd xxxx
node app.js

When instance is running and check my ec2 instance, there is no node js and nvm installed when I typed node -v

[ec2-user@ip-xxxx ~]$ nvm -v
-bash: nvm: command not found
[ec2-user@ip-xxxx ~]$ node -v
-bash: node: command not found

What I missed ? I wrote this script as I usually do in my instance and work.

profile pictureAWS
Rowaida
질문됨 2년 전1559회 조회
2개 답변
2

The user-data is ran as root -user so the NVM is now installed under root only. So when you run the curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash -script it gets installed under /root/.nvm/. Your code then assumes it's installed under /home/ec2-user/.nvm which is not true, so your nvm commands after that fail.

So you would need to either install the nvm and node system wide. Or just run commands as ec2-user in the script via "sudo -u ec2-user <command>" For example:

sudo -u ec2-user sh -c 'curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash'
sudo -u ec2-user sh -c '. ~/.nvm/nvm.sh && nvm install --lts'

Other option would be to wrap all the commands you want to run as ec2-user into shell script that you download from S3 or that you create the file inside user-data.

Also I don't see you running nvm install that would install Node on the machine.

profile pictureAWS
전문가
Toni_S
답변함 2년 전
0

try this command npm -v

profile picture
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠