Files
download/nodejs/install.sh
耗子 758e1c8799
All checks were successful
Generate Checksums / checksums (push) Successful in 46s
feat: 清理提交
2026-01-31 07:03:45 +08:00

96 lines
2.9 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
source <(curl -f -s --connect-timeout 10 --retry 3 https://dl.acepanel.net/public.sh)
if [ $? -ne 0 ]; then
echo "Download public.sh failed, please check the network or try again later."
exit 1
fi
slug=${1}
version=${2}
node_path="${setup_path}/server/nodejs/${slug}"
in_china=$(curl --retry 2 -m 10 -L -k https://www.qualcomm.cn/cdn-cgi/trace 2>/dev/null | grep -qx 'loc=CN' && echo "true" || echo "false")
# 架构映射
case ${ARCH} in
x86_64)
NODE_ARCH="x64"
;;
aarch64)
NODE_ARCH="arm64"
;;
esac
# 准备安装目录
rm -rf ${node_path}
mkdir -p ${node_path}
cd ${node_path}
# 下载Node.js安装包
NODE_TARBALL="node-v${version}-linux-${NODE_ARCH}.tar.xz"
if ${in_china}; then
# 阿里云镜像站不知道是不是抽风了2025年5月底之后就没更新了改用腾讯镜像站
NODE_URL="https://mirrors.tencent.com/nodejs-release/v${version}/${NODE_TARBALL}"
echo "Downloading ${NODE_TARBALL} from Tencent mirror..."
else
NODE_URL="https://nodejs.org/dist/v${version}/${NODE_TARBALL}"
echo "Downloading ${NODE_TARBALL} from nodejs.org..."
fi
aria2c -x8 \
-U "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36" \
-o ${NODE_TARBALL} \
${NODE_URL}
if [ $? -ne 0 ]; then
rm -rf ${node_path}
error "Failed to download Node.js ${version}"
fi
# 解压
tar -xJf ${NODE_TARBALL}
if [ $? -ne 0 ]; then
rm -rf ${node_path}
error "Failed to extract Node.js ${version}"
fi
# Node.js解压后会在node-v{version}-linux-{arch}目录下,移动到当前目录
mv node-v${version}-linux-${NODE_ARCH}/* .
rmdir node-v${version}-linux-${NODE_ARCH}
rm -f ${NODE_TARBALL}
# 验证安装
if [ ! -f "${node_path}/bin/node" ]; then
rm -rf ${node_path}
error "Node.js ${version} installation failed"
fi
# 设置软链接
ln -sf ${node_path}/bin/corepack /usr/local/bin/corepack${slug}
ln -sf ${node_path}/bin/node /usr/local/bin/node${slug}
ln -sf ${node_path}/bin/npm /usr/local/bin/npm${slug}
ln -sf ${node_path}/bin/npx /usr/local/bin/npx${slug}
[ ! -f /usr/local/bin/corepack ] && ln -sf ${node_path}/bin/corepack /usr/local/bin/corepack
[ ! -f /usr/local/bin/node ] && ln -sf ${node_path}/bin/node /usr/local/bin/node
[ ! -f /usr/local/bin/npm ] && ln -sf ${node_path}/bin/npm /usr/local/bin/npm
[ ! -f /usr/local/bin/npx ] && ln -sf ${node_path}/bin/npx /usr/local/bin/npx
# 国内设置npm镜像
if ${in_china}; then
${node_path}/bin/npm config set --global registry https://registry.npmmirror.com
fi
# 创建环境变量配置
cat >${node_path}/env.sh <<EOF
export NODE_HOME=${node_path}
export PATH=\$NODE_HOME/bin:\$PATH
EOF
chmod 644 ${node_path}/env.sh
echo -e $HR
echo "Node.js ${version} installation successful"
echo "Node.js path: ${node_path}"
echo "To use this version, run: source ${node_path}/env.sh"
echo -e $HR