EC2 Userdata install node

0

The user data below failed to install nodejs on EC2 (following here ):

          #!/bin/bash

          # Install Git
          yum -y install git

          # Install Node.js
          curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
          . ~/.nvm/nvm.sh
          nvm install --lts
          node -e "console.log('Running Node.js ' + process.version)"
          npm install pm2 -g

          # MySQL client
          dnf update -y
          dnf install mariadb105 -y

          /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource PublicServerInstance --region ${AWS::Region}

The log shows that:

=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zprofile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
   OR
=> Append the following lines to the correct file yourself:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
/var/lib/cloud/instance/scripts/part-001: line 9: /root/.nvm/nvm.sh: No such file or directory
Xu Feng
질문됨 5달 전267회 조회
2개 답변
0

The error log is informative, and it seems to suggest that if you just create an empty ~/.bash_profile (or whatever file is appropriate for the shell you are running) then the NVM installer will populate the file with what it needs.

This seems to be confirmed by https://github.com/nvm-sh/nvm/issues/1837#issuecomment-397838000

There would also be value in considering this comment which is AWS-specific https://github.com/nvm-sh/nvm/issues/1837#issuecomment-1345499648

profile picture
전문가
Steve_M
답변함 5달 전
0

Hello.

I think the cause is that there is no profile data, as the error says.
You can solve the problem by creating an empty profile, or you can install it by setting the user data as shown below.
Therefore, you can install it by changing the user data as shown in the StackOverflow answer below.
https://stackoverflow.com/questions/54415841/nodejs-not-installed-successfully-in-aws-ec2-inside-user-data

#!/bin/bash

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
source ~/.bashrc
nvm install --lts
node -e "console.log('Running Node.js ' + process.version)"
npm install pm2 -g
version=$(node --version)
echo "export PATH=\$PATH:/.nvm/versions/node/$version/bin/" >> ~/.bashrc
profile picture
전문가
답변함 5달 전

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

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

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

관련 콘텐츠