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

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


当前回答

2022年5月

我在这上面花了太长时间。我的亚马逊Linux 2配置,以root身份运行。

#!/usr/bin/env zsh

# https://stackoverflow.com/questions/11542846/nvm-node-js-recommended-install-for-all-users
echo "=================================N=O=D=E========================================"

cd /usr/local/bin || exit 90

git clone https://github.com/nvm-sh/nvm.git .nvm

\. "/usr/local/bin/.nvm/nvm.sh"

nvm install --lts

node -e "console.log('Running Node.js ' + process.version)"

cat << "EOF" > /etc/profile.d/npm.sh
#!/usr/bin/env bash
export NVM_DIR="/usr/local/bin/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm'}

EOF

chmod 755 /etc/profile.d/npm.sh

npm install -g npm

2022年6月-系统真的很讨厌箱子里没有链接的东西。如果你需要其他用户可以访问的东西,这里有一个小更新。诚然,添加/etc/profile.d/npm.sh正是nvm建议的,但我发现它被高估了。我认为它可以被消去取代ln -s。快乐的黑客

#!/bin/zsh

# https://stackoverflow.com/questions/11542846/nvm-node-js-recommended-install-for-all-users
echo "=================================N=O=D=E========================================"

cd /usr/local/bin || exit 90

git clone https://github.com/nvm-sh/nvm.git .nvm

# this uncontrolled script has an unbound variable $HOME
# @link https://github.com/Drop-In-Gaming/dropingaming.com/runs/6437329820?check_suite_focus=true
\. "/usr/local/bin/.nvm/nvm.sh" || true

# todo - try to install 18
nvm install --lts

nvm install 17

node -e "console.log('Running Node.js ' + process.version)"

cat << "EOF" > /etc/profile.d/npm.sh
#!/usr/bin/env bash
export NVM_DIR="/usr/local/bin/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm'}

EOF

echo 'source /etc/profile.d/npm.sh' >> /root/.bashrc

echo 'source /etc/profile.d/npm.sh' >> /root/.zshrc

echo 'source /etc/profile.d/npm.sh' >> /home/ssm-user/.bashrc

echo 'source /etc/profile.d/npm.sh' >> /home/ssm-user/.zshrc

echo 'source /etc/profile.d/npm.sh' >> /home/www-data/.bashrc

echo 'source /etc/profile.d/npm.sh' >> /home/www-data/.zshrc

chmod 755 /etc/profile.d/npm.sh

npm install -g npm

echo "===========================WHERE==IS==NODE==========================="

which node

which npm

echo "symlinking to /usr/bin/"

if [ -e /usr/bin/node ]; then

  sudo rm -f /usr/bin/node

fi

if [ -e /usr/bin/npm ]; then

  sudo rm -f /usr/bin/npm

fi

sudo ln -s "$(which node)" /usr/bin/

sudo ln -s "$(which npm)" /usr/bin/


其他回答

您可以通过重新安装已安装的包到当前版本来更新/安装节点,这可能会使我们在进行更新时避免大量错误。

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

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

它也可以在AWS Linux实例上工作。

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

sudo yum install nodejs npm——enablerepo=epel适用于Amazon Linux AMI。 Curl—silent—location https://rpm.nodesource.com/setup_6.x | bash - Yum -y安装nodejs 在红帽公司工作。

https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

Curl—silent—location https://rpm.nodesource.com/setup_10.x | sudo bash - Sudo yum -y安装nodejs

2022年5月

我在这上面花了太长时间。我的亚马逊Linux 2配置,以root身份运行。

#!/usr/bin/env zsh

# https://stackoverflow.com/questions/11542846/nvm-node-js-recommended-install-for-all-users
echo "=================================N=O=D=E========================================"

cd /usr/local/bin || exit 90

git clone https://github.com/nvm-sh/nvm.git .nvm

\. "/usr/local/bin/.nvm/nvm.sh"

nvm install --lts

node -e "console.log('Running Node.js ' + process.version)"

cat << "EOF" > /etc/profile.d/npm.sh
#!/usr/bin/env bash
export NVM_DIR="/usr/local/bin/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm'}

EOF

chmod 755 /etc/profile.d/npm.sh

npm install -g npm

2022年6月-系统真的很讨厌箱子里没有链接的东西。如果你需要其他用户可以访问的东西,这里有一个小更新。诚然,添加/etc/profile.d/npm.sh正是nvm建议的,但我发现它被高估了。我认为它可以被消去取代ln -s。快乐的黑客

#!/bin/zsh

# https://stackoverflow.com/questions/11542846/nvm-node-js-recommended-install-for-all-users
echo "=================================N=O=D=E========================================"

cd /usr/local/bin || exit 90

git clone https://github.com/nvm-sh/nvm.git .nvm

# this uncontrolled script has an unbound variable $HOME
# @link https://github.com/Drop-In-Gaming/dropingaming.com/runs/6437329820?check_suite_focus=true
\. "/usr/local/bin/.nvm/nvm.sh" || true

# todo - try to install 18
nvm install --lts

nvm install 17

node -e "console.log('Running Node.js ' + process.version)"

cat << "EOF" > /etc/profile.d/npm.sh
#!/usr/bin/env bash
export NVM_DIR="/usr/local/bin/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm'}

EOF

echo 'source /etc/profile.d/npm.sh' >> /root/.bashrc

echo 'source /etc/profile.d/npm.sh' >> /root/.zshrc

echo 'source /etc/profile.d/npm.sh' >> /home/ssm-user/.bashrc

echo 'source /etc/profile.d/npm.sh' >> /home/ssm-user/.zshrc

echo 'source /etc/profile.d/npm.sh' >> /home/www-data/.bashrc

echo 'source /etc/profile.d/npm.sh' >> /home/www-data/.zshrc

chmod 755 /etc/profile.d/npm.sh

npm install -g npm

echo "===========================WHERE==IS==NODE==========================="

which node

which npm

echo "symlinking to /usr/bin/"

if [ -e /usr/bin/node ]; then

  sudo rm -f /usr/bin/node

fi

if [ -e /usr/bin/npm ]; then

  sudo rm -f /usr/bin/npm

fi

sudo ln -s "$(which node)" /usr/bin/

sudo ln -s "$(which npm)" /usr/bin/