我已经看到了使用yum安装依赖项,然后从源代码安装Node.JS和NPM的文章。虽然这是可行的,但我觉得Node.JS和NPM都应该在公共回购的某个地方。

如何在AWS亚马逊Linux上一个命令安装Node.JS和NPM ?


当前回答

我有Node.js 6。我想安装Node.js 8.x。

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

sudo yum remove nodejs:卸载Node.jsx(我不知道这是否有必要) Curl—silent—location https://rpm.nodesource.com/setup_8.x | sudo bash - 须藤yum清洁所有 sudo yum makecache:重新生成元数据缓存(这在文档中没有,但yum一直尝试安装Node。jx 6。x,不成功,直到我发出这最后两个命令) sudo yum install nodejs:安装Node.js 8.x

其他回答

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

check git is installed git --version or install it via: sudo yum install git install gcc and openssl: sudo yum install gcc-c++ make sudo yum install openssl-devel clone the git repo into a directory called node (which you can remove later): git clone https://github.com/nodejs/node.git decide which version of node you want at https://github.com/nodejs/node/releases go to the node directory just created and install node cd node git checkout v6.1.0 - put your desired version after the v ./configure make sudo make install test that node is installed / working with either node --version or simply node (exit node via process.exit() or ^C x 2 or ^C + exit) check the npm version: npm --version and update if necessary via sudo npm install -g npm Optional: remove the node directory with rm -r node


注:

The accepted answer didn't work since sudo yum install nodejs --enablerepo=epel-testing returns the error: No package nodejs available. ...and sudo yum install nodejs --enablerepo=epel (ie without -testing) only gave very old versions. If you already have an old version of node installed you can remove it with: sudo npm uninstall npm -g ...since npm can uninstall itself sudo yum erase nodejs sudo rm -f /usr/local/bin/node (sudo yum rm nodejs in the accepted answer won't work as rm is not a valid yum command see yum --help) It's possible to clone the node repo via git clone git://github.com/nodejs/node.git rather than git clone https://github.com/nodejs/node.gitbut you may get a various errors (see here). If you already have a /node dir from a previous install, remove it before using the git clone command (or there'll be a conflict): rm -r node If you have trouble with any sudo npm... command - like sudo: npm: command not found and/or have permissions issues installing node packages without sudo, edit sudo nano /etc/sudoers and add :/usr/local/bin to the end of the line Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin so that it reads Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

接受的答案给了我节点0.10.36和npm 1.3.6,这是非常过时的。我从nodejs下载页面获取了最新的linux-x64 tarball,安装起来并不太难:https://nodejs.org/dist/latest/。

# start in a directory where you like to install things for the current user
(For noobs : it downloads node package as node.tgz file in your directlry)
curl (paste the link to the one you want from the downloads page) >node.tgz

现在将刚刚下载的tar文件压缩

tar xzf node.tgz

运行这个命令,然后将它添加到你的.bashrc:

export PATH="$PATH:(your install dir)/(node dir)/bin"

(例如:export PATH ="$PATH:/home/ec2-user/mydirectory/node/node4.5.0-linux-x64/bin")

更新npm(只有一次,不要添加到.bashrc):

npm install -g npm

注意-g的意思是全局的,对npm实例来说是全局的也就是我们刚刚安装的实例它仅限于当前用户。这将适用于npm“全局”安装的所有包。

正如亚马逊文档(在亚马逊EC2实例上设置Node.js)中所述,只需运行以下命令:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install --lts

完成了!

最简单的解决方案是这样的(以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

EC2-Instance的官方文档:https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html

 1. curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash
 2. . ~/.nvm/nvm.sh
 3. nvm ls-remote (=> find your version x.x.x =>) nvm install  x.x.x
 4. node -e "console.log('Running Node.js ' + process.version)"