Fail to install PNPM on Dockerfile using NodeJS & NPM

73 views Asked by At

I have base Docker image which I must use (thus I can't just type FROM node:20.11.1 for example). I try to install PNPM globally on my Dockerfile but it fails.

I created the following Dockerfile:

    FROM public.ecr.aws/docker/library/docker
    
    # Install NodeJS & NPM to image
    COPY --from=node:20.11.1 /usr/local/bin /usr/local/bin
    COPY --from=node:20.11.1 /usr/local/lib/node_modules /usr/local/lib/node_modules
    
    # Install PNPM
    RUN npm i -g [email protected]
    
    # ..... MORE IRRELEVANT CODE .....

However, when I build the image the RUN npm i -g [email protected] command fails with an error:


     => ERROR [pixel-api stage-0  4/12] RUN npm i -g [email protected]                                                                                            0.1s
    ------
     > [pixel-api stage-0  4/12] RUN npm i -g [email protected]:
    0.131 env: can't execute 'node': No such file or directory
    ------
    failed to solve: process "/bin/sh -c npm i -g [email protected]" did not complete successfully: exit code: 127

I must use the public.ecr.aws/docker/library/docker image, but I also need the container to have NodeJS, NPM, PNPM installed. I don't really mind the way, I just need these 3 binaries with the versions provided in the Dockerfile.

I also tried:

FROM node:20.11.1 as base

RUN npm i -g [email protected]

FROM public.ecr.aws/docker/library/docker

# Install NodeJS & NPM & PNPM to image
COPY --from=base /usr/local/bin /usr/local/bin
COPY --from=base /usr/local/lib/node_modules /usr/local/lib/node_modules

RUN ln -s /usr/local/bin/node /usr/bin/node
RUN ln -s /usr/local/bin/npm /usr/bin/npm
RUN ln -s /usr/local/bin/pnpm /usr/bin/pnpm

ENV PATH "/usr/local/bin:${PATH}"

# * https://stackoverflow.com/questions/75602063/pip-install-r-requirements-txt-is-failing-this-environment-is-externally-mana
ENV PIP_BREAK_SYSTEM_PACKAGES 1

RUN apk update && \
    apk add gcc py-pip python3-dev libffi-dev musl-dev && \
    pip install --upgrade pip setuptools wheel && \
    pip install --upgrade aws-sam-cli

WORKDIR /var/opt

COPY ./package.json ./pnpm-workspace.yaml ./.npmrc ./
COPY ./apps/pixel-api/package.json ./apps/pixel-api/

RUN pnpm i -w

And then I get same error for RUN pnpm i -w:

env: can't execute 'node': No such file or directory
1

There are 1 answers

3
Zoltan Kochan On

The easiest and probably most reliable way to install pnpm currently is using corepack: corepack enable pnpm

Corepack is shipped with Node.js.

More details: https://pnpm.io/installation#using-corepack