如何在Amazon Linux上安装Node.JS

node.js Node.js

十三逆天

2020-03-20

我看过有关使用yum安装依赖项,然后从源代码安装Node.JS和NPM的文章。虽然这样做确实可行,但我觉得Node.JS和NPM都应该放在某个公共仓库中。

如何在AWS Amazon Linux上的一个命令中安装Node.JS和NPM?

第2498篇《如何在Amazon Linux上安装Node.JS》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

12个回答
米亚 2020.03.20

对我有用的过程(遵循这些相当古老的说明并进行了一些更新):

  • 检查git是否已安装git --version或通过以下方式安装:
    sudo yum install git
  • 安装gcc和openssl:
    sudo yum install gcc-c++ make
    sudo yum install openssl-devel
  • 将git repo克隆到一个名为node(您可以稍后删除)的目录中:
    git clone https://github.com/nodejs/node.git
  • https://github.com/nodejs/node/releases上确定所需的节点版本
  • 转到刚刚创建的节点目录并安装节点
    cd node
    git checkout v6.1.0-将所需的版本放在v
    ./configure
    make
    sudo make install
  • 测试该节点安装/使用工作要么node --version或简单地node(经由出口节点process.exit()^CX 2或^C+ exit
  • 检查npm版本:npm --version并根据需要更新sudo npm install -g npm
  • 可选:使用以下命令删除node目录rm -r node

笔记:

  1. 接受的答案无效,因为sudo yum install nodejs --enablerepo=epel-testing返回了错误:No package nodejs available.
    ... and sudo yum install nodejs --enablerepo=epel(即不带-testing)仅给出了非常旧的版本。
  2. 如果您已经安装了旧版本的节点,则可以使用以下命令将其删除:
    sudo npm uninstall npm -g ...因为npm可以自行卸载
    sudo yum erase nodejs
    sudo rm -f /usr/local/bin/node
    sudo yum rm nodejs在接受的答案中,该rm命令无效,因为不是有效的yum命令,请参见yum --help
  3. 可以通过git clone git://github.com/nodejs/node.git而不是克隆节点存储库,git clone https://github.com/nodejs/node.git但是您可能会遇到各种错误(请参阅此处)。
  4. 如果您已经有/node以前安装目录,请在使用git clone命令之前将其删除(否则会发生冲突):
    rm -r node
  5. 如果您在使用任何sudo npm...命令时都遇到麻烦-例如sudo: npm: command not found和/或在没有sudo的情况下安装节点软件包有权限问题,请编辑sudo nano /etc/sudoers并添加:/usr/local/bin到该行的末尾,以Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin使其读取Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
斯丁前端 2020.03.20

像其他人一样,被接受的答案也给了我一个过时的版本。

这是另一种效果很好的方法:

$ curl --silent --location https://rpm.nodesource.com/setup_12.x | bash -
$ yum -y install nodejs

您也可以将12.x替换为另一个版本,例如10.x,8.x等。

您可以在NodeSource Github页面上查看所有可用版本,并根据需要从那里拉出。

注意:您可能需要sudo根据环境运行使用

米亚 2020.03.20

对于那些希望在Ansible中运行被接受的答案而无需进一步搜索的人,我将任务发布在此处,以方便和将来参考。

接受的答案建议:https//stackoverflow.com/a/35165401/78935

等效任务

tasks:
  - name: Setting up the NodeJS yum repository
    shell: curl --silent --location https://rpm.nodesource.com/setup_10.x | bash -
    args:
      warn: no
  # ...
逆天斯丁 2020.03.20

您可以通过将安装的软件包重新安装到当前版本来更新/安装节点,这可以在进行更新的同时避免很多错误。

这是由nvm使用以下命令完成的。在这里,我将节点版本更新为8,并将所有可用的软件包也重新安装到v8!

nvm i v8 --reinstall-packages-from=default

它也适用于AWS Linux实例。

飞云 2020.03.20

如官方文档中所述,只需简单的2个步骤即可-

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs
十三老丝 2020.03.20

我安装了Node.js 6.x,并想安装Node.js8.x。

这是我使用的命令(来自Nodejs的站点,还有一些额外的步骤来处理yum缓存的数据):

  1. sudo yum remove nodejs:卸载Node.js 6.x(我不知道这是否必要)
  2. curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
  3. sudo yum clean all
  4. sudo yum makecache:重新生成元数据缓存(文档中没有此内容,但是yum一直尝试安装Node.jx 6.x,但未成功,直到我发出了最后两个命令)
  5. sudo yum install nodejs:安装Node.js 8.x
乐米亚 2020.03.20

最简单的解决方案是(以root用户身份执行这些操作)

sudo su root
cd /etc
mkdir node
yum install wget
wget https://nodejs.org/dist/v9.0.0/node-v9.0.0-linux-x64.tar.gz
tar -xvf node-v9.0.0-linux-x64.tar.gz
cd node-v9.0.0-linux-x64/bin
./node -v
ln -s /etc/node-v9.0.0-linux-x64/bin/node node

在此处输入图片说明

猪猪Sam 2020.03.20

对于v4 LTS版本,请使用:

curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -
yum -y install nodejs

对于Node.js v6,请使用:

curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
yum -y install nodejs

尝试在Amazon Linux上安装本机插件时,我也遇到了一些问题。如果要执行此操作,还应该安装构建工具:

yum install gcc-c++ make
蛋蛋猿 2020.03.20

sudo yum install nodejs npm --enablerepo=epel适用于Amazon Linux AMI curl --silent --location https://rpm.nodesource.com/setup_6.x | bash - yum -y install nodejs 适用于RedHat。

StafanDavaidSam 2020.03.20

迷迷糊糊地发现,后来很难再次发现。后代放在这里:

sudo yum install nodejs npm --enablerepo=epel

编辑3:从2016年7月起,编辑1不再适用于nodejs 4(和编辑2都不适用)。这个答案(https://stackoverflow.com/a/35165401/78935)提供了一个真正的单线。

编辑1:如果您正在寻找nodejs 4,请尝试EPEL测试库:

sudo yum install nodejs --enablerepo=epel-testing

EDIT 2: To upgrade from nodejs 0.12 installed through the EPEL repo using the command above, to nodejs 4 from the EPEL testing repo, please follow these steps:

sudo yum rm nodejs
sudo rm -f /usr/local/bin/node
sudo yum install nodejs --enablerepo=epel-testing

The newer packages put the node binaries in /usr/bin, instead of /usr/local/bin.

And some background:

The option --enablerepo=epel causes yum to search for the packages in the EPEL repository.

EPEL (Extra Packages for Enterprise Linux) is open source and free community based repository project from Fedora team which provides 100% high quality add-on software packages for Linux distribution including RHEL (Red Hat Enterprise Linux), CentOS, and Scientific Linux. Epel project is not a part of RHEL/Cent OS but it is designed for major Linux distributions by providing lots of open source packages like networking, sys admin, programming, monitoring and so on. Most of the epel packages are maintained by Fedora repo.

Via http://www.tecmint.com/how-to-enable-epel-repository-for-rhel-centos-6-5/

古一 2020.03.20

似乎没有人提到这一点。Amazon Linux 2上,加载EPEL的官方方法是:

  • sudo amazon-linux-extras install epel

...那么您可以:

  • sudo yum install nodejs

     

请参阅附加程序库(Amazon Linux 2)

老丝阿飞 2020.03.20

使用NVM轻松安装...

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash
nvm install node

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android