【问题标题】:Docker: npm not foundDocker:未找到 npm
【发布时间】:2022-02-02 18:44:40
【问题描述】:

我有以下 Dockerfile:

FROM ubuntu
USER root
RUN apt-get update && apt-get install curl -y
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get update && apt-get upgrade -y && apt-get install nodejs -y
RUN mkdir /opt/public
RUN mkdir /opt/bin
ADD public /opt/public
ADD bin /opt/bin
RUN ls -lah /opt/bin
RUN ls -lah /opt/public
ADD run.sh /bin/run.sh
RUN chmod +x /bin/run.sh
RUN cd /opt/bin && npm install
CMD ["/bin/run.sh"]

当我构建容器时,我得到这个错误:

/bin/sh: 1: npm: 未找到

有什么问题?你能帮帮我吗?

【问题讨论】:

    标签: node.js docker npm build containers


    【解决方案1】:

    尝试在构建映像时单独安装npm

    RUN apt-get update && apt-get upgrade -y && \
        apt-get install -y nodejs \
        npm                       # note this one
    

    【讨论】:

    • 谢谢,我使用“apt-get install npm -y”而不是“npm install”。一切正常。
    • 当我运行 docker 时,我得到这个错误:找不到模块 'localstorage-polyfill' 你有什么解决办法吗?
    • 这可能是因为您将npm install 转换为apt-get install npm,而两者都需要
    • 你是对的。我在 Dockerfile 中都写了。一切都很好。
    • 对我不起作用,我必须先curl -sL https://deb.nodesource.com/setup_13.x | bash -
    【解决方案2】:

    Node 也打包了npm,所以不需要像 Yury 提到的那样安装npm。这样做通常是个坏主意,因为您无法控制 nodejsnpm 版本

    对我来说,答案很简单。我有以下代码:

    # install nodejs
    RUN curl --silent --location https://deb.nodesource.com/setup_12.x | bash -
    RUN apt-get install -y \
      nodejs
    RUN echo "Node: " && node -v
    RUN echo "NPM: " && npm -v
    
    

    但我必须安装 curl,所以它失败了。所以在此之前,你需要安装 curl:

    RUN apt-get update && apt-get install -y curl
    

    【讨论】:

    • 伟大的收获!我有同样的问题
    【解决方案3】:

    在运行任何 npm 命令之前尝试在 Docker 文件中添加这两行。

    RUN apt-get install --no-install-recommends apt-utils --yes \
        && apt-get install --no-install-recommends npm --yes
    

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 2017-01-27
      • 2018-10-09
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      • 2016-07-21
      相关资源
      最近更新 更多