You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker容器中使用Nodesource脚本安装Node.js出现弃用警告如何解决?

解决Debian容器Nodesource setup脚本弃用问题

Nodesource的setup_18.x脚本已被弃用,执行时会强制等待60秒,严重拖慢镜像构建速度。以下是几种可行的解决办法:

方法1:手动配置Nodesource APT源(推荐)

跳过弃用的setup脚本,直接添加官方源和密钥来安装Node.js 18:

RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl git nano ca-certificates
# 创建密钥存储目录并添加Nodesource GPG密钥
RUN mkdir -p /etc/apt/keyrings
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
# 写入Node.js 18的APT源配置
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
# 更新源并安装Node.js
RUN apt-get update -yq && apt-get install nodejs -yq

方法2:使用官方预编译二进制包

不依赖APT源,直接下载官方编译好的二进制包部署:

RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl git nano ca-certificates
# 下载并解压Node.js 18最新版到系统目录
RUN curl -fsSL https://nodejs.org/dist/latest-v18.x/node-$(curl -fsSL https://nodejs.org/dist/latest-v18.x/ | grep -oP 'node-\K[0-9]+\.[0-9]+\.[0-9]+' | head -1)-linux-x64.tar.xz | tar -xJ -C /usr/local --strip-components=1
# 验证安装结果
RUN node --version && npm --version

方法3:用nvm管理多版本(可选)

如果需要灵活切换Node.js版本,可使用nvm:

RUN apt-get update -yq && apt-get upgrade -yq && apt-get install -yq curl git nano ca-certificates
# 安装nvm
RUN curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# 加载nvm环境并安装Node.js 18
RUN export NVM_DIR="$HOME/.nvm" && [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" && \
    nvm install 18 && nvm alias default 18
# 配置环境变量,确保后续命令能直接调用node/npm
ENV PATH="/root/.nvm/versions/node/v18.x/bin:$PATH"

注意:方法3中nvm是用户级安装,若后续需要在容器启动时使用,建议在ENTRYPOINT或CMD中加载nvm环境。

内容的提问来源于stack exchange,提问作者Jesús López

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 17:53:19