cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] NPM with default config can't run on read-only env

Open ck-delivery-admin-vn opened this issue 2 years ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

This issue exists in the latest npm version

  • [X] I am using the latest npm

Current Behavior

NPM 10.x with default config not working when running in AWS lambda which only allows writing to /tmp folder.

ARG FUNCTION_DIR="/function"

FROM node:20-buster as build-image
ARG FUNCTION_DIR
RUN apt-get update && \
    apt-get install -y \
    g++ \
    make \
    cmake \
    unzip \
    libcurl4-openssl-dev \
    git

# npm can't run in read-only env (lambda)
# https://github.com/npm/cli/issues/5183
ENV NPM_CONFIG_CACHE=/tmp/.npm # <----------- need move cache to /tmp to fix the issue

# install lib
RUN mkdir -p ${FUNCTION_DIR}
COPY package.json package-lock.json ${FUNCTION_DIR}
WORKDIR ${FUNCTION_DIR}
RUN npm install
RUN npm install aws-lambda-ric

# copy code
COPY . ${FUNCTION_DIR}

ENTRYPOINT ["/usr/local/bin/npx", "aws-lambda-ric"]
CMD ["index.handler"]

The NPM will throw error and exit

npm ERR! code EROFS
npm ERR! syscall mkdir
npm ERR! path /home/sbx_user1051
npm ERR! errno -30
npm ERR! rofs EROFS: read-only file system, mkdir '/home/sbx_user1051'
npm ERR! rofs Often virtualized file systems, or other file systems
npm ERR! rofs that don't support symlinks, give this error.
npm ERR! Log files were not written due to an error writing to the directory: /home/sbx_user1051/.npm/_logs 

Related issue: https://github.com/npm/cli/issues/5183

Expected Behavior

NPM with default configurations running in read-only env should disable logging and caching to maintain functionality.

Steps To Reproduce

  1. Run NPM in read-only env (ex: Lambda, Linux with limited write permission...)
  2. Remove ENV NPM_CONFIG_CACHE=/tmp/.npm in Dockerfile
  3. Run Docker container will throw errors.

Environment

~ docker exec -it 46 /bin/bash                                                                                     huyda@huyda-ec2
root@467754e80479:/function# npm config ls
; "env" config from environment

cache = "/tmp/.npm"

; node bin location = /usr/local/bin/node
; node version = v20.10.0
; npm local prefix = /function
; npm version = 10.2.3
; cwd = /function
; HOME = /root
; Run `npm config ls -l` to show all defaults.

ck-delivery-admin-vn avatar Dec 26 '23 08:12 ck-delivery-admin-vn