npm / npm/cli

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

Open
#7,105 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug
Dominant language
JavaScript
Stars
10.1k
Forks
4.7k
Avg merge
2d 2h
Merged PRs (30d)
19

Description

Is there an existing issue for this?
  • I have searched the existing issues
This issue exists in the latest npm version
  • 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the failure from the Dockerfile steps in a read-only environment and inspect the output of npm config ls and npm config ls -l. Compare the default cache and logging behavior with the related issue #5183. Done means npm runs without attempting unwritable home-directory operations while preserving normal behavior in writable environments.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.