Install nodejs in python slim buster

Question:

I have a Dockerfile that starts with

FROM python:3.7-slim-buster

and I want to install node.js and npm in it. How can I install them in this image?

Asked By: David Masip

||

Answers:

This should work:

FROM python:3.7-slim-buster

# setup dependencies
RUN apt-get update
RUN apt-get install xz-utils
RUN apt-get -y install curl

# Download latest nodejs binary
RUN curl https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz -O

# Extract & install
RUN tar -xf node-v14.15.4-linux-x64.tar.xz
RUN ln -s /node-v14.15.4-linux-x64/bin/node /usr/local/bin/node
RUN ln -s /node-v14.15.4-linux-x64/bin/npm /usr/local/bin/npm
RUN ln -s /node-v14.15.4-linux-x64/bin/npx /usr/local/bin/npx

To run node start it with docker run -it <containerName> /bin/bash
Then node, npm and npx are available

Answered By: Michael Zigldrum

npm globally packages need to add links if you want to use it as command.

FROM python:3.7-slim-buster
ENV NODE_VERSION 14.15.4

#if build in `china`, debian mirrors, npm registry change to china source
ARG AREA=london

RUN set -ex 
    && if [ 'china' = "$AREA" ] ; then 
        sed -i "s@http://deb.debian.org@https://mirrors.aliyun.com@g" /etc/apt/sources.list; 
    fi 
    && apt-get update 
    && apt-get install -y git xz-utils curl 

    # install node
    && curl "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.xz" -O 
    && tar -xf "node-v$NODE_VERSION-linux-x64.tar.xz" 
    && ln -s "/node-v$NODE_VERSION-linux-x64/bin/node" /usr/local/bin/node 
    && ln -s "/node-v$NODE_VERSION-linux-x64/bin/npm" /usr/local/bin/npm 
    && ln -s "/node-v$NODE_VERSION-linux-x64/bin/npx" /usr/local/bin/npx 

    # npm install bump, openapi-generator
    && if [ 'china' = "$AREA" ] ; then 
        npm config set registry https://registry.npm.taobao.org/; 
    fi 
    && npm install -g [email protected] 
    && ln -s "/node-v$NODE_VERSION-linux-x64/bin/bump" /usr/local/bin/bump  

    # clear
    && npm cache clean --force 
    && rm -rf /var/lib/apt/lists/* 
    && rm -f "/node-v$NODE_VERSION-linux-x64.tar.xz" 
    && apt-get clean 
    && apt-get autoremove
Answered By: Song

Just user the official installation for Debian from here :

RUN curl -fsSL https://deb.nodesource.com/setup_19.x | bash - &&
   apt-get install -y nodejs
Answered By: Daniel Ben Zaken
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.