Sohrab
Sohrab

Reputation: 1478

Docker: npm not found

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?

Upvotes: 23

Views: 81693

Answers (8)

Raheel Hasan
Raheel Hasan

Reputation: 6023

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.

Upvotes: 1

Andressa Silva
Andressa Silva

Reputation: 31

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 -

Upvotes: 3

Bill Horvath
Bill Horvath

Reputation: 1717

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

Upvotes: 3

Brijesh Ray
Brijesh Ray

Reputation: 1263

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

Upvotes: 1

user9888797
user9888797

Reputation: 99

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

Upvotes: 1

Sumit Bajaj
Sumit Bajaj

Reputation: 39

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

Upvotes: 0

bersling
bersling

Reputation: 19262

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

Upvotes: 7

Yury Fedorov
Yury Fedorov

Reputation: 14928

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

Upvotes: 44

Related Questions