Docker:npm未找到

22

我有以下的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: not found

问题出在哪里?你能帮我解决一下吗?

8个回答

38

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

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

1
谢谢,我使用了“apt-get install npm -y”而不是“npm install”。一切正常。 - Sohrab
2
当我运行docker时,出现了这个错误:找不到模块'localstorage-polyfill'。你有什么解决方案吗? - Sohrab
1
可能是因为你把 npm install 改成了 apt-get install npm,但你需要两个命令都执行。 - Yury Fedorov
1
你是对的。我在Dockerfile中都写了。一切正常。 - Sohrab
1
对我来说没有用,我必须先运行 curl -sL https://deb.nodesource.com/setup_13.x | bash - - fabpico
1
@FabianPicone 我也更喜欢那个选项,但别忘了安装curl,否则你会得到上面概述的错误 :) - bersling

7

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

太棒了!我遇到了同样的问题。 - tplusk

3
如果有人继续遇到这个问题,很可能是由于镜像底层操作系统的软件包管理器指定了一个非常旧的Node版本,它不包括npm。以下是针对Dockerfile的链接答案的修改版本:
# This is needed to update the OS' package manager so that 
# the current version of node will be installed:
RUN curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -

RUN apt-get -yq update \
    && apt-get -yq upgrade \
    && apt-get install -yq nodejs \
    && npm --version

3
在我的情况下,一切都运行正常,直到我更新了Docker并开始出现这个错误。所以在尝试了上述所有解决方案但没有一个有效的情况下,我改变了node镜像的版本,这对我起作用。
之前:
RUN curl -fsSL https://deb.nodesource.com/setup_17.x | bash -

在之后:
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash -

1
令人惊讶的是,这个方法解决了我的代码问题。我的 Dockerfile 已经运行良好很长时间,但突然间出现了 npm 找不到的错误。非常感谢! - Cristian Favaro Carriço

1

您可能已经在这里安装了nodenpm。也许您需要在通过curl安装节点包后,在新的交互式shell中运行与npm/node相关的脚本。因此,在最后一行,您可以尝试:

CMD cat /bin/run.sh | bash -ic

或者

CMD bash -i /bin/run.sh

或者

CMD ["/bin/bash","-i","/bin/run.sh"]

在我的情况下,交互式的npm/node bash是可行的,并使用bash -i调用


这是对我有效的解决方案。RUN npm install 可以工作,这意味着它已经安装好了。这个神奇的 -i 不知怎么地起了作用。 - diaa

1

在构建Docker容器时,我遇到了以下错误:

> [runtime  1/11] RUN curl -sL https://deb.nodesource.com/setup_16.x | -E bash;        apt-get install -y nodejs;        npm i -g npm@8:
#11 0.360 /bin/sh: 1: -E: not found
#11 1.687 Reading package lists...
#11 1.696 Building dependency tree...
#11 1.698 Reading state information...
#11 1.703 E: Unable to locate package nodejs
#11 1.703 /bin/sh: 1: npm: not found

err: appname
/bin/sh: 1: npm: not found

在 Dockerfile 中我进行了更改:

  RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - \
      && apt-get install -y nodejs \
      && npm i -g npm@8

到这里

RUN apt-get update && apt-get upgrade -y && \
    apt-get install -y nodejs \
    npm   

容器构建成功


0

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

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

0

这是在2023年实际操作的方法,如果你想在默认使用较新版本的Debian版本上获取旧的Node版本。例如,如果你需要在Debian 10上通过Nodesource获取Node 8。

RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get install -y nodejs=8.17.0-1nodesource1

如果您不添加 8.17.0-1nodesource1,它将仍会安装与 Debian 10 提供的默认版本。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接