skip to Main Content

When I run npm run development I am getting the error:

Creating api_node_run ... done

> dev
> npm run development

npm WARN logfile Error: EACCES: permission denied, scandir '/root/.npm/_logs'
npm WARN logfile  error cleaning log files [Error: EACCES: permission denied, scandir '/root/.npm/_logs'] {
npm WARN logfile   errno: -13,
npm WARN logfile   code: 'EACCES',
npm WARN logfile   syscall: 'scandir',
npm WARN logfile   path: '/root/.npm/_logs'
npm WARN logfile }

> development
> mix

npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /root/.npm/_cacache/tmp
npm ERR! errno -13
npm ERR! 
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR! 
npm ERR! To permanently fix this problem, please run:
npm ERR!   sudo chown -R 1000:33 "/root/.npm"

npm ERR! Log files were not written due to an error writing to the directory: /root/.npm/_logs
npm ERR! You can rerun the command with `--loglevel=verbose` to see the logs in your terminal
npm notice 
npm notice New major version of npm available! 8.19.4 -> 10.2.5
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.2.5
npm notice Run npm install -g [email protected] to update!
npm notice 

I have a Docker Node container defined in docker-compose.yml as:

node:
    image: connectedplaces/api/node
    build:
      context: ./docker/node
      dockerfile: Dockerfile
    volumes:
      - .:/var/www/html

The Dockerfile is:

# Set base image.
FROM node:16.20

# Install git for faster package downloads.
RUN apt-get install -y git

RUN chown -R node:node /root/.npm

# Set the working directory to the project root.
WORKDIR /var/www/html

The container will launch successfully, and if I check the permissions with:

docker-compose run --rm --entrypoint ""  node ls -lart /root/.npm

The output is:

drwxr-xr-x 1 node node 4096 Sep  7 22:33 _logs
drwx------ 1 root root 4096 Sep  7 22:33 ..
drwxr-xr-x 1 node node 4096 Sep  7 22:33 .

I followed the ideas from Node Docker can't run dev command but I still cannot get around this error.
I have deleted the local image and rebuilt it, I have bumped the Node version in the Dockerfile and rebuilt, but nothing seems to work.

Any ideas what to do next?

2

Answers


  1. Chosen as BEST ANSWER

    After trying every solution suggested the only one that worked was to create a new cache directory and set npm_config_cache to it in the Dockerfile:

    # Set base image.
    FROM node:16.20
    
    # Install git for faster package downloads.
    RUN apt-get install -y git
    
    RUN mkdir -p /home/node/app/.npm 
    && chown -R node:node /home/node/app/.npm
    
    ENV npm_config_cache /home/node/app/.npm
    
    # Set the working directory to the project root.
    WORKDIR /var/www/html
    

    This was suggested in Error "Your cache folder contains root-owned files, due to a bug in previous versions of npm" while "npx create-react-app example_app" by @ASHBRN-PHNX Thanks


  2. Instead of "chowning" everything so it fits, it should make things better by using the "node" users uid and gid as build arguments. Consider:

    FROM node:16.20
    ARG UID=1001
    ARG GID=1001
    
    RUN groupmod -g ${GID} node && usermod -u ${UID} -g ${GID} node
    
    USER node
    
    WORKDIR /var/www/html
    

    and

    services:
      node:
        build:
          context: .
          dockerfile: Dockerfile
          args:
            - UID=501
            - GID=501
        volumes:
          - .:/var/www/html
    

    for example.

    This way you could use the host users uid and gid for development (On macos it is often 501, on Ubuntu 1000/1001 I think). In prod – ypu can just use the default.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search