首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NodeJS未成功安装在用户数据中的AWS EC2中

NodeJS未成功安装在用户数据中的AWS EC2中
EN

Stack Overflow用户
提问于 2019-01-29 07:25:54
回答 3查看 5.2K关注 0票数 7

我尝试在AWS NodeJS linux中安装带有nvmnvm,如下所示:

代码语言:javascript
复制
#!/bin/bash

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
source ~/.bashrc
nvm install 7

在成功创建实例并在我的ec2实例中输入和检查之后,当我键入nodejsnvm --version时,没有安装nodejsnvm

代码语言:javascript
复制
[ec2-user@ip-0-0-0-0 ~]$ node --version
-bash: node: command not found
[ec2-user@ip-0-0-0-0 ~]$ nvm --version
-bash: nvm: command not found

当我检查实例的日志时,会发现下面的错误消息。

代码语言:javascript
复制
[   16.310115] cloud-init[3300]: => Downloading nvm as script to '/.nvm'
[   17.053885] cloud-init[3300]: => Profile not found. Tried  (as defined in $PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
[   17.076402] cloud-init[3300]: => Create one of them and run this script again
[   17.087459] cloud-init[3300]: => Create it (touch ) and run this script again
[   17.092307] cloud-init[3300]: OR
[   17.100669] cloud-init[3300]: => Append the following lines to the correct file yourself:
[   17.117606] cloud-init[3300]: export NVM_DIR="$HOME/.nvm"
[   17.124904] cloud-init[3300]: [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[   17.161419] cloud-init[3300]: => Close and reopen your terminal to start using nvm or run the following to use it now:
[   17.177964] cloud-init[3300]: export NVM_DIR="$HOME/.nvm"
[   17.185400] cloud-init[3300]: [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-01-29 08:39:33

正如日志所解释的那样,install.sh脚本试图定位一个它找不到的概要文件。(请记住,用户数据中提供的脚本是以root形式运行的,所以$HOME是/root

解决方案是确保配置文件在安装前存在,或者按照日志消息中的建议,在安装后手动更改路径。

解决方案1 (未经测试)

代码语言:javascript
复制
#!/bin/bash

touch ~/.bashrc # this ensure the bashrc file is created
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
source ~/.bashrc
nvm install 7

解决方案2 (测试)

代码语言:javascript
复制
#!/bin/bash

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install 7

(当从用户数据运行时,$HOME is /)我在Amazon上的交互式会话中测试了上面的内容。

代码语言:javascript
复制
$ ssh ec2-user@ec2-18-202-174-164.eu-west-1.compute.amazonaws.com
Warning: Permanently added 'ec2-18-202-174-164.eu-west-1.compute.amazonaws.com,18.202.174.164' (ECDSA) to the list of known hosts.

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
3 package(s) needed for security, out of 3 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-172-31-30-44 ~]$ sudo bash
[root@ip-172-31-30-44 ec2-user]# curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 10250  100 10250    0     0  10250      0  0:00:01 --:--:--  0:00:01 54521
=> Downloading nvm as script to '/root/.nvm'


=> Appending source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="/root/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
[root@ip-172-31-30-44 ec2-user]#
[root@ip-172-31-30-44 ec2-user]# export NVM_DIR="$HOME/.nvm"
[root@ip-172-31-30-44 ec2-user]# [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[root@ip-172-31-30-44 ec2-user]# nvm install 7
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v7.10.1 (npm v4.2.0)
Creating default alias: default -> 7 (-> v7.10.1)
[root@ip-172-31-30-44 ec2-user]# node --version
v7.10.1

请注意,上面将为根用户安装nvmnodenpm。它不会在ec2-user的环境中添加正确的ENV。要做到这一点,请以ec2-user登录,然后任一种类型。

代码语言:javascript
复制
export NVM_DIR="/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

或者把这个添加到ec2-user.bashrc

它起作用的证据(以ec2-user登录):

代码语言:javascript
复制
[ec2-user@ip-172-31-20-26 ~]$ export NVM_DIR="/.nvm"
[ec2-user@ip-172-31-20-26 ~]$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ec2-user@ip-172-31-20-26 ~]$ node --version && npm --version
v7.10.1
4.2.0

您可以在您的user-data脚本中自动化这一点:

代码语言:javascript
复制
cat <<EOF >> /home/ec2-user/.bashrc
export NVM_DIR="/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
EOF
票数 11
EN

Stack Overflow用户

发布于 2022-08-19 09:06:31

如果我们使用的是amazon,请尝试安装nvm版本16。

代码语言:javascript
复制
#!/bin/bash
sudo su
cd ~
amazon-linux-extras install nginx1 -y 
systemctl enable nginx
systemctl start nginx
touch ~/.bashrc
cat > /tmp/startup.sh << EOF
echo "Setting up NodeJS Environment"
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.39.0/install.sh | bash
echo 'export NVM_DIR="/home/ec2-user/.nvm"' >> /home/ec2-user/.bashrc
echo 'export NVM_DIR="/home/ec2-user/.nvm"' >> /home/ec2-user/.bash_profile
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm' >> /home/ec2-user/.bashrc
. /home/ec2-user/.nvm/nvm.sh
. /home/ec2-user/.bash_profile
. /home/ec2-user/.bashrc
# Install NVM, NPM, Node.JS & Grunt
nvm install 16
nvm ls
EOF
chown ec2-user:ec2-user /tmp/startup.sh && chmod a+x /tmp/startup.sh
sleep 1; su - ec2-user -c "/tmp/startup.sh"
票数 1
EN

Stack Overflow用户

发布于 2022-10-05 21:43:30

在做了几个小时的练习后,这件事对我起了作用。

代码语言:javascript
复制
#!/bin/bash
touch ~/.bashrc
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
source ~/.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install --lts
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54415841

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档