Getting Error: EACCES: permission denied, open '/usr/local/lib/node_modules
when trying to install a global module in docker:
FROM node:latest
RUN mkdir -p /code
RUN npm i -g npm
WORKDIR /code
RUN npm set progress=false && npm install -g exp
There is no information about it in the official docs for node or in https://forums.docker.com/
Per the current docker-node documentation, you can set the location of the global npm dependencies to a user-writeable location by adding these lines to the Dockerfile:
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
# optionally if you want to run npm global bin without specifying path
ENV PATH=$PATH:/home/node/.npm-global/bin
Npm does not allow running as root by default for security reasons. When you run npm as root (this is the default user in Docker build) and install a global package, npm installs and executes binaries as user nobody
instead, who doesn't have any permissions.
You can avoid this by adding the --unsafe-perm
flag:
RUN npm install --global --unsafe-perm exp
or by setting the global user explicitly to root
:
RUN npm --global config set user root && \
npm --global install exp
source
or by switching to a non-root USER during docker build. Be aware that this will affect ownership of files in your container.
The USER instruction sets the user name (or UID) and optionally the user group (or GID) to use when running the image and for any RUN, CMD and ENTRYPOINT instructions that follow it in the Dockerfile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With