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 個月前檢視次數 268 次
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 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南