Skip to content

Fix Docker Permission Denied Error for node_modules


While trying to develop a React application running inside a Docker container, I ran into an error with permissions for the node_modules folder.

After running the Docker container, it displayed the following error:

[eslint] EACCES: permission denied, mkdir ‘/app/node_modules/.cache’

ERROR in [eslint] EACCES: permission denied, mkdir ‘/app/node_modules/.cache’

That was from a project using Create React App.

I faced a similar result using Vite instead:

Error: EACCES: permission denied, mkdir ‘/app/node_modules/.vite/deps_temp’ at Object.mkdirSync (node:fs:1382:3)

So the problem clearly was something with my Docker configuration.

The Dockerfile I used is a slight modification of the official Node.js Docker example.

FROM node:16-alpine


COPY package*.json ./

RUN npm install

COPY . ./


CMD [ "npm", "start" ]

Of course, since I was trying to actively develop the application, I needed a way to update the code inside the container.

So, I created a docker-compose.yml and added 2 volumes to sync local code changes with the container.

    build: .
      - ./:/app
      - /app/node_modules
      - 3000:3000

But, as I mentioned before, running docker compose up produced the aforementioned permission error with node_modules folder.

I connected to the container and checked the file permissions.

docker exec -it app sh
ls -la

And saw that node_modules belonged to root while the rest of the files belong to node.

drwxr-xr-x    1 root     root         21534 Mar  6 10:13 node_modules
-rw-r--r--    1 node     node       1211893 Mar  6 09:01 package-lock.json
-rw-r--r--    1 node     node           810 Mar  6 09:01 package.json
drwxr-xr-x    1 node     node           132 Mar  6 09:01 public
drwxr-xr-x    1 node     node           160 Mar  6 09:01 src

The fix to this problem is giving node user permissions to the node_modules directory.

To give permissions, update your Dockerfile:

FROM node:16-alpine

RUN mkdir /app && chown node:node /app

USER node
COPY --chown=node:node package.json package-lock.json* ./

RUN npm install

COPY --chown=node:node . .


CMD [ "npm", "start" ]

I used the example Dockerfile of node-docker-good-defaults GitHub repository. I recommend you to check out its Dockerfile and docker-compose.yml example files and learn more on your own.

For the changes to apply, you might need to bring the container down and clean up its volumes first.

You can do that by running docker compose down -v.

To rebuild the image, you can run docker compose up --build.

Now that you have given the node user ownership over the node_modules directory, the permission error should be resolved.