22

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
  • 1,348
  • 4
  • 13
  • 33

8 Answers8

37

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
Yury Fedorov
  • 14,508
  • 6
  • 50
  • 66
7

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
bersling
  • 17,851
  • 9
  • 60
  • 74
3

In my case everything was working fine until I update the Docker and start getting this error. So after try all the above solutions and none of them work, I changed the version of node image and this worked for me.

Before:

RUN curl -fsSL https://deb.nodesource.com/setup_17.x | bash -

After:

RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
HiroCereal
  • 550
  • 1
  • 11
2

In case anyone continues to run across this problem, it's likely due to the package manager on the image's underlying OS specifying a version of node that's so old that it doesn't include npm. Here's a modified version of the linked answer for a 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
Bill Horvath
  • 1,336
  • 9
  • 24
1

You perhaps have already installed node and npm here. May be you need to run npm/node related script in a new interactive shell, after installing node packages through curl. So, in the last line, you may try:

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

Or

CMD bash -i /bin/run.sh

Or

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

Interactive bash for npm/node worked in my case and is invoked with bash -i

  • This is the solution that worked for me. `RUN npm install` works, which means it is already installed. This magical `-i` worked somehow. – diaa Oct 25 '22 at 21:22
1

I was getting following error while building docker container:

> [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

In dockerfile i changed:

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

to this

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

and container build succeeded

Brijesh Ray
  • 779
  • 8
  • 15
0

Try adding these two lines in Docker file before running any npm command.

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

This is how you actually do it in 2023 if you are tying to get old node version on a debian version that defaults to newer version of node. for example, if you need node 8 on debian 10 via nodesource.

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

if you dont add 8.17.0-1nodesource1 it will still install the default ver available with debian 10.

Raheel Hasan
  • 5,753
  • 4
  • 39
  • 70