Docker: npm not found

26,485

Solution 1

Try installing npm separately while building the image:

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

Solution 2

Node also packages npm, so no need to install npm like mentioned by Yury. It's in general a bad idea to do it like that, because you don't have control over the nodejs and npm version

For me the answer was quite simple. I had the following code:

# 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

but I for got to install curl, so it failed. So before this, you need to install curl:

RUN apt-get update && apt-get install -y curl
Share:
26,485
Sohrab
Author by

Sohrab

Updated on July 05, 2022

Comments

  • Sohrab
    Sohrab almost 2 years

    I have the following 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"]
    

    When I build the Container, I get this eror:

    /bin/sh: 1: npm: not found

    What is the problem? Could you please help me?

  • Sohrab
    Sohrab over 5 years
    Thanks, I used "apt-get install npm -y" instead of "npm install". Everything is ok.
  • Sohrab
    Sohrab over 5 years
    When I run docker, I get this error: Cannot find module 'localstorage-polyfill' Do you have any solution?
  • Yury Fedorov
    Yury Fedorov over 5 years
    It's probably because you cnahged npm install to apt-get install npm, while you need both
  • Sohrab
    Sohrab over 5 years
    You are right. I wrote both in the Dockerfile. Everything is fine.
  • fabpico
    fabpico over 4 years
    Did not work for me, i had to curl -sL https://deb.nodesource.com/setup_13.x | bash - first.
  • bersling
    bersling over 3 years
    @FabianPicone I also prefer that option, but don't forget to install curl, or you'll get exactly the error outlined above :)
  • tplusk
    tplusk about 3 years
    Great catch! I had the same gotcha