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
asked 5 months ago245 views
2 Answers
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
EXPERT
Steve_M
answered 5 months ago
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
EXPERT
answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions