Docker: npm not found
Asked Answered
L

8

23

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?

Leaving answered 20/3, 2019 at 10:4 Comment(0)
S
42

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
Spinous answered 20/3, 2019 at 10:8 Comment(6)
Thanks, I used "apt-get install npm -y" instead of "npm install". Everything is ok.Leaving
When I run docker, I get this error: Cannot find module 'localstorage-polyfill' Do you have any solution?Leaving
It's probably because you cnahged npm install to apt-get install npm, while you need bothSpinous
You are right. I wrote both in the Dockerfile. Everything is fine.Leaving
Did not work for me, i had to curl -sL https://deb.nodesource.com/setup_13.x | bash - first.Atonsah
@FabianPicone I also prefer that option, but don't forget to install curl, or you'll get exactly the error outlined above :)Hett
H
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
Hett answered 10/3, 2021 at 7:47 Comment(1)
Great catch! I had the same gotchaFillagree
S
3

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
Saez answered 8/2, 2023 at 19:17 Comment(0)
M
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 -
Maidamaidan answered 30/6, 2023 at 14:41 Comment(1)
Surprisingly this fixed my code. My Dockerfile was working for ages but suddenly started showing the npm not found. Thank you!Bonehead
K
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

Kalsomine answered 18/5, 2022 at 12:24 Comment(1)
This is the solution that worked for me. RUN npm install works, which means it is already installed. This magical -i worked somehow.Beveridge
R
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

Rathe answered 24/11, 2022 at 9:58 Comment(0)
C
1

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.

Capsicum answered 14/8, 2023 at 7:13 Comment(0)
S
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
Safe answered 2/2, 2022 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.