Docker: npm not found - node.js

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?

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

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

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

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

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

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

Related

yum in dockerfile - There are no enabled repos

I am having issues with installing python3 with yum in dockerfile. I did look on internet, I did try few things, not working. Its just small thing but not able to figure it out. When I try to build below docker file I do get error . I get error at line -
RUN yum install -y oracle-epel-release-el7
There are no enabled repos.
Run "yum repolist all" to see the repos you have.
You can enable repos with yum-config-manager --enable <repo>
The docker file is below.
FROM openjdk:13-jdk-slim
ARG MAVEN_VERSION=3.6.3
ARG USER_HOME_DIR="/root"
ARG SHA=c35a1803a6e70a126e80b2b3ae33eed961f83ed74d18fcd16909b2d44d7dada3203f1ffe726c17ef8dcca2dcaa9fca676987befeadc9b9f759967a8cb77181c0
ARG BASE_URL=https://apache.osuosl.org/maven/maven-3/${MAVEN_VERSION}/binaries
# Install prerequisites
RUN apt-get update && apt-get install -y curl
RUN apt-get update && apt-get install -y yum
RUN mkdir -p /usr/share/maven /usr/share/maven/ref \
&& curl -fsSL -o /tmp/apache-maven.tar.gz ${BASE_URL}/apache-maven-${MAVEN_VERSION}-bin.tar.gz \
&& echo "${SHA} /tmp/apache-maven.tar.gz" | sha512sum -c - \
&& tar -xzf /tmp/apache-maven.tar.gz -C /usr/share/maven --strip-components=1 \
&& rm -f /tmp/apache-maven.tar.gz \
&& ln -s /usr/share/maven/bin/mvn /usr/bin/mvn
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"
# Install Python 3.6
RUN yum install -y oracle-epel-release-el7
RUN yum install -y python36
# Install AWS CLI
RUN pip3 install awscli
CMD ["/bin/bash"]
Why mix apt and yum? You're already using apt-get, just use it to install your python too:
apt-get install python3.6

Jenkins slave with nodejs to run npm publish

I have a dockerfile where I install nodejs. I create a container from that dockerfile to use as a slave in jenkins. The problem is that when I try to publish (npm publish --registry ...) it says that I need to login first. But I cannot use npm adduser in a jenkins file cause you have to enter user and password manually when it is required. I have tried to add the file .nmrw with a valid token, but it haven't worked. Any idea?
Best regards.
FROM jenkins/ssh-slave
# Install selected extensions and other stuff
RUN apt-get update && apt-get -y --no-install-recommends install && apt-get clean
RUN apt-get install -y curl
# Install nodejs
RUN curl -sL https://deb.nodesource.com/setup_8.x | bash -
RUN apt-get install -y nodejs && apt-get install -y nginx
The key is in the file .npmrc. You have to have the same server you publish to.
FROM jenkins/ssh-slave
# Install selected extensions and other stuff
RUN apt-get update && apt-get -y --no-install-recommends install && apt-get clean
RUN apt-get install -y curl
ARG NODEJS_VERSION=setup_11.x
# Install nodejs
RUN curl -sL https://deb.nodesource.com/${NODEJS_VERSION} | bash - && apt-get install -y nodejs && apt-get install -y nginx
RUN chown -R jenkins:jenkins /home/jenkins && chmod -R 777 /home/jenkins
COPY .npmrc /home/jenkins/.npmrc
RUN npm -v

Dockerfile ubuntu only installs node version 4.2

This dockerfile installs nodejs version 4.2 and I cant understand why. could someone please help me install node 9.2. i've tried taking out the -- no install-recommends command to no avail.
adding more text her because stack would not let me post this even though it is a very simple question that I've looked on the web for quite some time about to no avail.adding more text her because stack would not let me post this even though it is a very simple question that I've looked on the web for quite some time about to no avail.
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y --no-install-recommends curl sudo
RUN curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
RUN apt-get install -y nodejs && \
apt-get install --yes build-essential
RUN apt-get install --yes npm
#VOLUME "/usr/local/app"
# Set up C++ dev env
RUN apt-get update && \
apt-get dist-upgrade -y && \
apt-get install gcc-multilib g++-multilib cmake wget -y && \
apt-get clean autoclean && \
apt-get autoremove -y
#wget -O /tmp/conan.deb -L https://github.com/conan-io/conan/releases/download/0.25.1/conan-ubuntu-64_0_25_1.deb && \
#dpkg -i /tmp/conan.deb
#ADD ./scripts/cmake-build.sh /build.sh
#RUN chmod +x /build.sh
#RUN /build.sh
RUN mkdir -p /usr/local/app
WORKDIR /usr/local/app
COPY package.json /usr/local/app
RUN ["npm", "install"]
COPY . .
RUN echo "/usr/local/app/dm" > /etc/ld.so.conf.d/mythrift.conf
RUN echo "/usr/lib/x86_64-linux-gnu" >> /etc/ld.so.conf.d/mythrift.conf
RUN echo "/usr/local/lib64" >> /etc/ld.so.conf.d/mythrift.conf
RUN ldconfig
RUN chmod +x dm/dm3
RUN ldd dm/dm3
RUN ["chmod", "+x", "dm/dm3"]
RUN ["chmod", "777", "policy"]
RUN ls -al .
RUN ["nodejs", "-v"]
CMD ["nodejs", "-v"]
EDIT
Apparently it's important for the OP to run exactly this version of ubuntu. Here's a sample that builds on top of FROM ubuntu:16.04:
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y --reinstall ca-certificates curl build-essential \
&& curl -s https://nodejs.org/dist/v9.9.0/node-v9.9.0-linux-x64.tar.xz \
-o node-v9.9.0-linux-x64.tar.xz && tar xf node-v9.9.0-linux-x64.tar.xz \
&& cd node-v9.9.0-linux-x64 && cp -r bin include lib share /usr/local \
&& rm -rf /node-v9.9.0-linux-x64.tar.xz /node-v9.9.0-linux-x64
CMD ["node", "-v"]
Build
docker build -t testing .
Test
docker run testing
v9.9.0
Note that this only takes care of the node related things and don't take into account all the other dependencies.
The reason you are getting node 4 is because apt-get only installs the default version of a package which will never be the cutting edge latest.
Whilst this issue is present in a Docker container, it is not specific to Docker as it will happen on any Ubuntu installation, both inside or outside of Docker.
To get the latest version you have 2 options.
(1) Install using a PPA:
cd ~
curl -sL https://deb.nodesource.com/setup_9.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt-get install nodejs
nodejs -v
(2) Install using Node Version Manager (nvm)
The latter is great because it lets you install multiple versions of Node and jump between them very quickly.
Here's a link to an amazing Digital Ocean article on this very topic:
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-16-04
Here's a link to NVM ... https://github.com/creationix/nvm

Package 'npm' has no installation candidate

I want to install npm in Debian 9.
I've tried:
apt-get install nodejs
completely installed and nodejs -v results: v4.8.2.
but when try to run npm an error says:
bash: npm: command not found
Base on my searches I've tried other ways:
based on this guide: https://www.godaddy.com/help/install-nodejs-ubuntu-17395 I've tried:
sudo apt-get install npm
results:
E: Package 'npm' has no installation candidate
Based on another guide I've tried:
wget https://npmjs.org/install.sh
sudo chmod +x install.sh
sudo ./install.sh
results:
npm cannot be installed without node.js.
of course I've installed nodejs. I also tried this way:
curl --silent --location https://deb.nodesource.com/setup_0.12 | sudo bash -
sudo apt-get update
sudo apt-get install --yes nodejs
It says:
nodejs is already the newest version (4.8.2~dfsg-1).
What should I do?
Remove the old version
sudo apt-get purge nodejs
Install the new version:
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
sudo apt-get install -y nodejs
Alternatively, for Node.js 9:
curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
sudo apt-get install -y nodejs
Source
It might tell you that you are missing package gnupg2, just install it.

Install node in Dockerfile?

I am user of AWS elastic beanstalk, and I have a little problem. I want to build my CSS files with less+node. But I don`t know how to install node in my dockerfile, when building with jenkins.
Here is installation packages what I am using in my docker. I will be glad for any suggestions.
FROM php:5.6-apache
# Install PHP5 and modules along with composer binary
RUN apt-get update
RUN apt-get -y install \
curl \
default-jdk \
git \
libcurl4-openssl-dev \
libpq-dev \
libmcrypt-dev \
libpq5 \
npm \
node \
zlib1g-dev \
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng12-dev
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/
RUN docker-php-ext-install curl json mbstring opcache pdo_mysql zip gd exif sockets mcrypt
# Install pecl
RUN pecl install -o -f memcache-beta \
&& rm -rf /tmp/pear \
&& echo 'extension=memcache.so' > /usr/local/etc/php/conf.d/memcache.ini
After this I am runing my entrypoint.sh with code
#!/usr/bin/env sh
composer run-script post-install-cmd --no-interaction
chmod 0777 -R /var/app/app/cache
chmod 0777 -R /var/app/app/logs
exec apache2-foreground
But then I`ve got this error
Error Output: [2016-04-04 11:23:44] assetic.ERROR: The template ":tmp:module.html.twig" contains an error: A template that extends another one cannot have a body in ":tmp:module.ht
ml.twig" at line 7.
But when I install inside the Docker container node this way
apt-get install git-core curl build-essential openssl libssl-dev
git clone https://github.com/nodejs/node.git
cd node
./configure
make
sudo make install
node -v
I can build my CSS. So question is..how this installation above make install inside my Dockerfile when I am building it with Jenkins?
I think this works slightly better.
ENV NODE_VERSION=16.13.0
RUN apt install -y curl
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN node --version
RUN npm --version
Note that nvm is a version manager for node.js, designed to be installed per-user, and invoked per-shell. nvm works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: unix, macOS, and windows WSL.
Running apt-get install node does not install Node.js, because that's not the package you're asking for.
If you run apt-cache info node you can see that what you are installing is a "Amateur Packet Radio Node program (transitional package)"
You should follow the Node.js install instructions to install via package manager.
Or if you like building from git, you can just do that inside Docker:
RUN apt-get install -y git-core curl build-essential openssl libssl-dev \
&& git clone https://github.com/nodejs/node.git \
&& cd node \
&& ./configure \
&& make \
&& sudo make install
According to the following answer, I would suggest using npm via the n package, that lets you choose the nodejs version, or use the latest tag or the lts tag. For example for latest:
RUN apt-get update && apt-get install -y \
software-properties-common \
npm
RUN npm install npm#latest -g && \
npm install n -g && \
n latest
Just 2 lines
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs
Get the node image and put it at the top of your dockerfile:
FROM node:[tag_name] AS [alias_name]
Verify the version by adding following code:
RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version
Then add the following code every time you need to use nodejs in a container:
COPY --from=[alias_name] . .
From the codes above, replace the following with:
[tag_name] - the tag value of the node image you want to use. Visit https://hub.docker.com/_/node?tab=tags for the list of available tags.
[alias_name] - your preferred image name to use in your dockerfile.
Example:
FROM node:latest AS node_base
RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version
FROM php:5.6-apache
COPY --from=node_base . .
### OTHER CODE GOES HERE
Binary download without any compilation
FROM ubuntu
RUN apt-get update && apt-get install -y \
ca-certificates \
curl
ARG NODE_VERSION=14.16.0
ARG NODE_PACKAGE=node-v$NODE_VERSION-linux-x64
ARG NODE_HOME=/opt/$NODE_PACKAGE
ENV NODE_PATH $NODE_HOME/lib/node_modules
ENV PATH $NODE_HOME/bin:$PATH
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz | tar -xzC /opt/
# comes with npm
# RUN npm install -g typescript
I am using following Dockerfile to setup node version 8.10.0.
Here I have used NVM (Node Version Manager ), so we can choose which node version should be installed on that container. Please use absolute path of npm when installing node modules (eg: /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm install leasot#latest -g)
FROM ubuntu:18.04
ENV NODE_VERSION=8.10.0
RUN apt-get update && \
apt-get install wget curl ca-certificates rsync -y
RUN wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
RUN cp /root/.nvm/versions/node/v${NODE_VERSION}/bin/node /usr/bin/
RUN cp /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm /usr/bin/
RUN /root/.nvm/versions/node/v${NODE_VERSION}/bin/npm install leasot#latest -g
Note: This is a cropped Dockerfile.
The short answer, for example, install v14.17.1
ENV PATH="/opt/node-v14.17.1-linux-x64/bin:${PATH}"
RUN curl https://nodejs.org/dist/v14.17.1/node-v14.17.1-linux-x64.tar.gz |tar xzf - -C /opt/
list of all available versions can be found here -> https://nodejs.org/dist/
Directly into /usr/local so it's already in your $PATH
ARG NODE_VERSION=8.10.0
RUN curl https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz | tar -xz -C /usr/local --strip-components 1
The accepted answer gives the link to the installation instructions for all systems, but it won't run out of the box since you often (e.g. for ubuntu) don't have all required dependencies installed (namely curl and sudo).
So here's for example how you'd do it for ubuntu:
FROM ubuntu
# Core dependencies
RUN apt-get update && apt-get install -y curl sudo
# Node
# Uncomment your target version
# RUN curl -fsSL https://deb.nodesource.com/setup_10.x | sudo -E bash -
# RUN curl -fsSL https://deb.nodesource.com/setup_12.x | sudo -E bash -
# RUN curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
# RUN curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
RUN sudo apt-get install -y nodejs
RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version
then build with
docker build . --progress=plain
to see the output of the echo statements. Of course you could also leave away the echo statements and run it regularly with docker build ., after you've made sure everything is working as intended.
You can also leave away the installation of sudo, but then you'll have to get rid of the sudo occurrences in the script.
FROM ubuntu:20.04
# all necessaries for next RUN
RUN set -e; \
apt-get update && \
apt-get install -qqy --no-install-recommends \
curl wget nano gnupg2 software-properties-common && \
rm -rf /var/lib/apt/lists;
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
# uncomment for checking versions
# Step 4/10 : RUN apt-cache show nodejs | grep Version;return 1;
# ---> Running in xxxxxxxxx
# Version: 14.18.2-deb-1nodesource1
# Version: 10.19.0~dfsg-3ubuntu1
#RUN apt-cache show nodejs | grep Version;return 1;
RUN set -e; \
apt-get update && \
apt-get install -qqy \
nodejs && \
rm -rf /var/lib/apt/lists;
# uncomment for check
# RUN node -v

Resources