NPM logfile error

Nuh

Upgrading projects is not as straightforward a business as npm upgrade all especially JS ecosystems (and I know .NET too 🤣). You never know which dependency is going to refuse being in touch with the times, and more worryingly, which ones are going to give you false leads.

I encountered this small problem when I decided to upgrade my Gatsby site’s version to v4.

 npm WARN logfile Error: EACCES: permission denied, scandir '/root/.npm/_logs'

How did it happen? As is almost the case with this current upgrade that is taking place via Docker, the issue stayed in Docker.

How did I resolve it? I tried this

RUN chown -R /root/.npm/_logs

It didn’t work, and I tried doing it on local which also failed. At last, I came across this discussion and decided to test it anyway. The key, to my understanding, was to create a user (or alias) called “node” after the node image is installed by referencing it with chown during each copy process so that the files will be under “node” ownership.

Then just before the commands for running the app are executed, set the USER to be “node”.

FROM node:17

WORKDIR /app

ENV PATH /app/node_modules/.bin:$PATH

COPY --chown=node:node package*.json .

RUN npm install -g npm@8.5.2

COPY --chown=node:node . ./

USER node

CMD ["npm", "start"]
Nuh © 2024