Q: How to install global npm dependencies in docker? 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/

A: 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


A: This is an issue between npm and exp module. You can install the module using yarn instead
MODULE 72: Module._load REQUEST path parent: /usr/local/lib/node_modules/exp/node_modules/decompress-zip/lib/file-details.js
MODULE 72: load native module path
ngrok - error unpacking binary { Error: EACCES: permission denied, open '/usr/local/lib/node_modules/exp/node_modules/@expo/ngrok/bin/ngrok'
  errno: -13,
  code: 'EACCES',
  syscall: 'open',
  path: '/usr/local/lib/node_modules/exp/node_modules/@expo/ngrok/bin/ngrok' }
npm info lifecycle @expo/ngrok@2.2.8~postinstall: Failed to exec postinstall script
npm WARN react-redux@5.0.6 requires a peer of react@^0.14.0 || ^15.0.0-0 || ^16.0.0-0 but none was installed.

MODULE 38: Module._load REQUEST os parent: /usr/local/lib/node_modules/npm/lib/utils/error-handler.js
MODULE 38: load native module os

But you can install it using yarn
$ yarn global add exp

See below issue for more detail
https://github.com/expo/exp/issues/59

A: 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.


A: You can try login as node or root user before install command, add USER before RUN command:
USER node

or
USER root

