npm permission issue in laravel docker setup

I faced the same issue while compiling assets form inside a docker container running a laravel app.

Background: I created the app to run as a docker container(following instructions on laravel docs), which uses laravel sail to build the containers. After creating and running the containers, i logged into the app container(i.e.;laravel.test).

  • Inside, I ran npm install to install node dependencies.
  • Ran npm run dev While webpack was able to successfully compile the assets, it also produced an error:
    glob error [Error: EACCES: permission denied, scandir '/root/.npm/_logs'] {
      errno: -13,
      code: 'EACCES',
      syscall: 'scandir',
      path: '/root/.npm/_logs'
    }

Since the assets were successfully compiled, i assumed it's a notice-ish error, nothing major. Still i wanted to see if i could get rid of it.

How i got rid of it: When I entered the container, I was logged in as root. I looked up to see what other users are available in the system. I run cat /etc/passwd and noticed this line:

sail:x:1000:1000::/home/sail:/bin/bash

Which means sail was a user on the system. So on the shell, i ran su sail to change into sail user. Then ran npm run dev and the process finished successfully without producing that error.

    root@f0fec81c3439:/var/www/html# su sail
    sail@f0fec81c3439:/var/www/html$ npm run dev