Docker:找不到 npm

Soh*_*rab 8 containers build node.js npm docker

我有以下 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"]
Run Code Online (Sandbox Code Playgroud)

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

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

问题是什么?请你帮助我好吗?

Yur*_*rov 14

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

RUN apt-get update && apt-get upgrade -y && \
    apt-get install -y nodejs \
    npm                       # note this one
Run Code Online (Sandbox Code Playgroud)

  • 当我运行 docker 时,出现此错误: Cannot find module 'localstorage-polyfill' 你有什么解决方案吗? (2认同)

ber*_*ing 7

Node还有软件包npm,所以不需要npm像 Yury 提到的那样安装。一般来说,这样做是一个坏主意,因为您无法控制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

Run Code Online (Sandbox Code Playgroud)

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

RUN apt-get update && apt-get install -y curl
Run Code Online (Sandbox Code Playgroud)